Using ColdFusion to parse a list with empty values

I was parsing through a csv file today and having issues with empty values. The data I’m receiving is in a format the 3rd party cannot update, so my data looked something like this

1,,abc,32,,gef

Dumping the list provides the expected list, but looping through the list it skips over values

<cfoutput>
  <cfloop list="#list1#" index="i">
    #i#<br>
  </cfloop>
</cfoutput>

produces

1
abc
32
gef

After googling this for a bit I gave up and asked my brother. He told me to use the ‘includeemptyfields’ attribute in listtoarray(). I knew I could turn a list into an array, but I wasn’t aware of the third attribute until today, the default is obviously false, so blank values were never included.

The code below produces the desired output. Notice I am now looping through an array instead of a list.

<cfset newlist = listToArray(list1,',',true) />
<cfoutput>
  <cfloop array="#newlist#" index="i">
    #i#<br>
  </cfloop>
</cfoutput>

1

abc
32

gef

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>