4 Replies Latest reply on Mar 25, 2010 10:20 AM by Fernis

    Reading from CSV

    emmim44 Level 1

      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>

        • 1. Re: Reading from CSV
          ilssac Level 5

          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 ||.

          • 2. Re: Reading from CSV
            emmim44 Level 1

            Thank u Ian

            • 3. Re: Reading from CSV
              ilssac Level 5

              ianskinner wrote:

               

              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 in the tush when you do not want it to do this.

               

              As of ColdFusion 9, there is not now 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 roll 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 quotes '' or pipes ||.

               

              I am glad I was able to help, but I just had to correct some of my typos for posterity.

              • 4. Re: Reading from CSV
                Fernis Level 3

                >As of ColdFusion 9, there is not a parameter in the list functions to  tell ColdFusion

                 

                There is NOT? Well, the docs prove you right. There isn't.

                 

                Actually, I know you meant "now", not "not". There is. And for ListLen(), this is an undocumented feature. It's a bug in the CFML reference.

                 

                (Edit: Damn, I didn't notice the reply, where the obvious typo was discussed. Old jokes are old. But there's still a bug in the docs)

                 

                -Fernis