Dave
Dave

Reputation: 977

Using csvToArray and loop through results with coldfusion

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.

Upvotes: 1

Views: 1325

Answers (2)

duncan
duncan

Reputation: 31920

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>

Upvotes: 4

Adam Cameron
Adam Cameron

Reputation: 29870

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.

Upvotes: 1

Related Questions