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.
listAppend, right? Then you could check if the value is there, then use it else use''.arrayIsDefined()looks off.... Please post a dump of the element throwing the error, iecsvArray[2].ialong with some text and a<br>tag.