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

One thought on “Using ColdFusion to parse a list with empty values

  1. I build my list in a form like this…

    Then I enter numbers for the “we” values. If I fill it with a zero, it works fine. If I delete the number that was in the field, and process it, it does not work.

    When I process it, I use… w is an array variable.

    If there were 4 elements in the list, and one was blank, it just returns 3. It does not return and empty string.

    This is the problem. Hope you can help.

Leave a Reply

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