Copy link to clipboard
Copied
Maybe I just don't see it, but I can;t seem to get this to work.
- I'm trying to parse through a giant CSV file from mailchimp, just grabbing a couple of bits of data from each line.
- I'm treating the file as a list of lists, 1 list per line.
- for some reason the list index does not work, i.e. listgetat(list, 1, ',') works, but, listgetat(list,2,',') gives me an invalid index error
BUT
I can loop over the list using a <cfloop> tag with no index errors!!??!!
See example:
// get rid of header line
<cfset variables.csvContent = listDeleteAt(variables.csvContent, 1, ']') />
// remove the line qualifiers & replace with a line feed
<cfset variables.csvContent = replace(variables.csvContent, '[', '', 'all') />
<cfset variables.csvContent = replace(variables.csvContent, ']', '#Chr(13)#', 'all') />
// loop over the main list using the line feed as a delimiter
<cfloop list="#variables.csvContent#" delimiters="#Chr(13)#" index="i">
// this works, it shows each row with the correct number of elements (12)
#i# - #listlen(i,',')#
// here is a sample row:
//"email@domain.com","fname","lname","company","group","uid",2,"",null,"date","IP","date"
// this DOES work, I get the first element [an email addy]
#listGetAt(i,1,',')#
// this does not work, I get an invalid index error
#listGetAt(i,2,',')#
// this DOES work, I get each of the 12 list elements returned.
<cfloop list="#i#" delimiters="," index="j" >
#j#
</cfloop>
I just don't see what I am doing wrong here.....
-help!
Copy link to clipboard
Copied
You seem to have used the correct syntax (though I'm not so sure about chr(13)). Strange.
I would try it with these versions instead:
<cfset variables.csvContent = replace(variables.csvContent, ']', Chr(13)&Chr(10), 'all') />
delimiters="#Chr(13)##Chr(10)#"
listlen(i)
listGetAt(i,1)
listGetAt(i,2)
<cfloop list="#i#" index="j" >.
Copy link to clipboard
Copied
You might have a line with an email address only. If there's a line in variables.csvContent with only an email address then you'll get the Invalid List Index error because CF ignores consecutive delimiters.
For example, if you have a line that looks like this...
"email[at]host.com",,,,,,,,,,
then there is only 1 list element in that line.
Copy link to clipboard
Copied
To add to BrianKeszler's comment. List functions have trouble with records which contain empty fields such as:
LastName,FirstName,Email,IdNumber
Smith,Jon,jon@example.com,1
Tyler,Rose,,2 <!--- this row has an empty email address field --->
You could try using the ListToArray function which has an option to treat omitted list elements as empty items rather than ignoring them.
ListToArray function:
http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-7f0f.html
Copy link to clipboard
Copied
List functions have trouble with records which contain empty fields
They're getting a lot better. listGetAt() will respect empty elements now, if told to do so:
http://help.adobe.com/en_US/ColdFusion/9.0/CFMLRef/WSc3ff6d0ea77859461172e0811cbec22c24-6d78.html
--
Adam
Copy link to clipboard
Copied
ok - I figured it out - as I thought, simple.....
the loop was getting to the end of the file, grabbing the next line delineated by "]" and finding a zero length string.... [last line is empty]
fix was simple enough, changed "]" to "[" so that the script looked for the START of a line.
-thanks guys