Skip to main content
Inspiring
March 16, 2010
Answered

Reading from CSV

  • March 16, 2010
  • 1 reply
  • 1112 views

Hi all, I am reading from a text file but some of the columns of csv may not have data. Is there a way to skip those?

I am getting an error ...

n function ListGetAt(list, index [, delimiters]), the value of index, 43, is not a valid as the first argument (this list has 42 elements). Valid indexes are in the range 1 through the number of elements in the list.

------------------------------------

    <cffile action="read" file="#CurrDir#ResourceList.csv" variable="csvfile">
    <cfset Validate = createobject("component", "rms_t.ValidateResource")>
     <cfset msg = "">
         <cfset counter = 1>
    
     <cfloop index="index" list="#ListRest(csvfile)#" delimiters="#chr(10)##chr(13)#">

          <cfif listgetAt('#index#',43) neq "" and listgetAt('#index#',43) eq 'Y'>         </cfif>

This topic has been closed for replies.
Correct answer ilssac

Actually the problem is that ColdFusion list functions have long had the feature of ignoring empty elements.  I.E. ColdFusion sees the list A,B,,C,,D,E as five items long not seven.  There is a valid, if somewhat esoteric, reason for this behavior but it is a real pain the tush when you do not want it to do this.

As of ColdFusion 9, there is not a parameter in the list functions to tell ColdFusion to count the empty list elements.  But if you are working with CF8 or earlier, you will have to handle the situation yourself.  There are functions to do this for you on the cflib.org web site.  But if you care to role your own, the common technique is to run a replace on the list putting some type of data between the empty list delimiters that you know represents an empty element, but CF sees as a list value.  Commonly empty single quote '' or pipe ||.

1 reply

ilssac
ilssacCorrect answer
Inspiring
March 16, 2010

Actually the problem is that ColdFusion list functions have long had the feature of ignoring empty elements.  I.E. ColdFusion sees the list A,B,,C,,D,E as five items long not seven.  There is a valid, if somewhat esoteric, reason for this behavior but it is a real pain the tush when you do not want it to do this.

As of ColdFusion 9, there is not a parameter in the list functions to tell ColdFusion to count the empty list elements.  But if you are working with CF8 or earlier, you will have to handle the situation yourself.  There are functions to do this for you on the cflib.org web site.  But if you care to role your own, the common technique is to run a replace on the list putting some type of data between the empty list delimiters that you know represents an empty element, but CF sees as a list value.  Commonly empty single quote '' or pipe ||.

emmim44Author
Inspiring
March 16, 2010

Thank u Ian