1
votes

I'm trying to read through one column of a CSV file and add all of the data in it to a list. After uploading the CSV file, I use a function called CSVToArray to turn the CSV file into a 2D array. Then, I create an empty list and loop through the CSV file, adding the data from a certain column in each row to my list.

<cfset myList = "">

<cfloop index="i" from=2 to=#ArrayLen(csvArray)#>
    <cfoutput>#csvArray[2][7]#</cfoutput>
    <cfset myList = listAppend(myList, csvArray[2][7], true)>
</cfloop> 

The code above gives me the following error on the line with the "listAppend" function, but not on the previous line which uses cfoutput to display the same value. The element at position 7, of dimension 2, of an array object used as part of an expression, cannot be found.

Is there a way for me to use listAppend even when the value I'm trying to append is null/empty?

EDIT: I'm likely just going to check if the array value exists before appending it using the ArrayIsDefined() function. New code:

<cfset myList = "">

<cfloop index="i" from=2 to=#ArrayLen(csvArray)#>
    <cfif arrayIsDefined(csvArray[i][7])>
        <cfset myList = listAppend(myList, csvArray[i][7])>
    </cfif>
</cfloop> 

I'm still curious as to why I'm getting the error from the listAppend() but not the cfoutput. Can anyone tell me why this happens?

EDIT 2: Here's a cfdump of csvArray[2]:

: array
1   12345
2   John
3   [empty string]
4   Smith
5   [empty string]
6   02/04/2020
7   [empty string]
8   [empty string]
9   [empty string]
10  [empty string]
11  [empty string]
12  [empty string]
13  [empty string]
14  [empty string]
15  123456789
16  [empty string]

EDIT 3: I tried it again with the following code to output the value being read from the CSV file along with the value for i, the iterator in my loop.

<cfloop index="i" from=2 to=#ArrayLen(csvArray)#>
    <cfoutput>#csvArray[i][7]#</cfoutput>__<Cfoutput>#i#</Cfoutput><br>
</cfloop>

Output:

__2
__3
__4
__5
__6

Error Occurred While Processing Request
The element at position 7, of dimension 2, of an array object used as part of an expression, cannot be found.

It's failing when it tries to output csvArray[7][7], which makes sense as the CSV file has only 6 lines. It is strange that it tells me it's failing on csvArray[2][7], unless I'm misunderstanding the error message.

1
The issue is with the array, not listAppend, right? Then you could check if the value is there, then use it else use ''. - rrk
I suppose that's true. I'm mostly confused that ColdFusion can find the element at [7][2] for a cfoutput, but it can't find the same thing when using listAppend. I think I'm going to use arrayIsDefined() to check if the value exists before appending it to my list. - HDuck
What version of CF? The syntax for arrayIsDefined() looks off.... Please post a dump of the element throwing the error, ie csvArray[2]. - SOS
I'm using ColdFusion 2018. (The dump didn't format right in a comment, adding it to my post) - HDuck
In the original loop, did you really have both array positions hard coded? Also, did the output tag actually output anything? It might be easier to figure out if you also output the value of i along with some text and a <br> tag. - Dan Bracuk

1 Answers

1
votes

Another approach is to read the csv file with the cfhttp tag, using the name attribute. That produces a query object. From there you can treat each column as a 1 dimensional array, and use the ArrayToList() function to create your list.

Here is a simple example.

<cfhttp url = "path to your csv file" 
name = "QueryObject" 
firstrowasheaders = "yes">

<cfset myList = ArrayToList(QueryObject["name of 7th column"])>

Edit starts here

Simpler yet, use the ValueList() function.