1

I am using some code that i got from Ben Nadel

I have added in some code to use my .csv file

<cffile action="READ" file="C:/ColdFusion10/cfusion/wwwroot/kelly2/debitorders.csv" variable="FileContent" result="csvfileupload">
<cfset CSVArray = FileContent>

<cfdump var="#CSVArray#">

<cfsavecontent variable="csv">
csvfileupload
</cfsavecontent>

<!--- Parse the test data. --->
<cfset result = csvToArray(
csv = trim( CSVArray )
) />

<!--- Output the results. --->
<cfdump
var="#result#"
label="Array 1"
/>

I would like to loop through the results however when I use

<cfloop array="#result#" index="i">
<cfoutput>#i#</cfoutput>
</cfloop>

I get the following error:

"Complex object types cannot be converted to simple values.

The expression has requested a variable or an intermediate expression result as a simple value. However, the result cannot be converted to a simple value. Simple values are strings, numbers, boolean values, and date/time values. Queries, arrays, and COM objects are examples of complex values."

I would also like to know how I can refer to each of these items and their columns, not sure if my terminology makes any sense

My end goal is to go through the csv and read it line by line, sorting out the data depending on a variable within the line and then writing it to a new .txt file. Please let me know if I am on the right track or should rather do something different.

1
  • 2
    csvToArray returns a 2D array as can be clearly seen in the screenshots on Ben's article, you'll need an inner loop if you want to access each individual value. Commented Nov 20, 2014 at 13:12

2 Answers 2

4

csvToArray returns a 2D array as can be clearly seen in the screenshots on Ben's article, and his comment "We are going to create an array of arrays in which each nested array represents a row in the CSV data file."

You'll need an inner loop if you want to access each individual value.

<cfloop array="#result#" index="i">
    <cfloop array="#i#" index="j">
        <cfoutput>#j#</cfoutput>
    </cfloop>
</cfloop>
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks duncan that works perfectly! would you happen to know how i can refer to specific items in the array? I have a row (branch) in the original csv and i need to check its value to go further
If you know you need to check row x, you can refer to it directly: <cfloop array="#results[99]#" index="j">
1

Well: what's the value of i? Did you check? You didn't say so if you did.

It's more than likely going to be a struct or some other complex data type, which cannot simply be output (which is what the error says).

Use <cfdump> to check the value of i, and then deal with it appropriate according to what it contains. It's tricky to say anything more without you providing that information.

2 Comments

Hi Adam, with coldfusion the index = i as ive named it in the <cfloop> tag
Thanks Dave, I know how CFML works. I was asking you to troubleshoot your code by checking the value of i. Which you clearly hadn't done.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.