2

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.

7
  • 1
    The issue is with the array, not listAppend, right? Then you could check if the value is there, then use it else use ''. Commented May 4, 2020 at 16:43
  • 1
    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. Commented May 4, 2020 at 16:48
  • What version of CF? The syntax for arrayIsDefined() looks off.... Please post a dump of the element throwing the error, ie csvArray[2]. Commented May 4, 2020 at 21:50
  • I'm using ColdFusion 2018. (The dump didn't format right in a comment, adding it to my post) Commented May 5, 2020 at 20:39
  • 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. Commented May 6, 2020 at 2:40

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm likely going to rework my code to use this solution - it seems a lot cleaner than looping through the CSV array.
I'm trying to use cfhttp now to read the file, but I'm having some issues. Am I able to use this to read a file that's been uploaded to the server? It tells me I need to set the URL attribute, and I'm not sure what to set it to. I tried setting it to the file path of the file I'm trying to read, but when I do that I get an error saying that QueryObject (or whatever I put for the name attribute) doesn't exist.
The following link shows a code sample for reading a csv file with cfhttp. coldfusion-example.blogspot.com/2009/07/…

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.