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
Matt Busche's Picture

About Matt Busche

Software Engineer and Wheel of Fortune Expert If this article helped you, please consider buying me a book.

Des Moines, IA https://www.mrbusche.com