• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 330
  • Last Modified:

ColdFusion data output for a text file

I am trying to take data from a text file and output it on the screen in a table.  I get all of the data to output but I cannot figure this error out that shows up on the bottom of the page:

 Invalid list index 2.
In function ListGetAt(list, index [, delimiters]), the value of index, 2, is not a valid as the first argument (this list has 1 elements). Valid indexes are in the range 1 through the number of elements in the list.
 
The error occurred in D:\Inetpub\test\test\test.cfm: line 36

34 :
35 : test1 = ListGetAt(orec, 1,"|");
36 : test2 = ListGetAt(orec, 2,"|");  <- Line of error
37 : test3 = ListGetAt(orec, 3,"|");
38 : test4 = ListGetAt(orec, 4,"|");


Here is my code:

<cfset file ="fakefile.txt">
<cfset path = "D:\inetpub\fakefile\fakefile.txt">
<cfoutput>#Path#</cfoutput><br />

<cfif FileExists("#path#") is "Yes">

  File Is There: <br />
  <cfelse> no file
</cfif>  
<CFFILE ACTION="READ" FILE="#Path#" VARIABLE="datavar">
<CFSET crlf = "~">
<br>
<table width="100%"  border="1" cellspacing="2" cellpadding="3">
  <tr>
   <td><font size="1" face="Verdana, Arial, Helvetica, sans-serif">id 1</font></td>
    <td><font size="1" face="Verdana, Arial, Helvetica, sans-serif">id 2</font></td>
       <td><font size="1" face="Verdana, Arial, Helvetica, sans-serif">id 3</font></td>
    <td><font size="1" face="Verdana, Arial, Helvetica, sans-serif">id 4</font></td>
    <td><font size="1" face="Verdana, Arial, Helvetica, sans-serif">id 5</font></td>
    <td><font size="1" face="Verdana, Arial, Helvetica, sans-serif">id 6</font></td>
    <td><font size="1" face="Verdana, Arial, Helvetica, sans-serif">id 7 </font></td>
      <td><font size="1" face="Verdana, Arial, Helvetica, sans-serif">id 8</font></td>
      
        
 
  </tr>

<cfoutput>
<cfset tot=  0>
<!--- Set the variable CountVar to 0. --->
<CFLOOP INDEX="orec" LIST="#datavar#" DELIMITERS="#crlf#">

 <CFSCRIPT>

id1 = ListGetAt(orec, 1,"|");
id2 = ListGetAt(orec, 2,"|");
id3 = ListGetAt(orec, 3,"|");
id4 = ListGetAt(orec, 4,"|");
id5 = ListGetAt(orec, 5,"|");
id6 = ListGetAt(orec, 6,"|");
id7 = ListGetAt(orec, 7,"|");
id8 = ListGetAt(orec, 8,"|");

</CFSCRIPT>


 <tr bgcolor="##00FF99">
 <td width="62"><font size="1" face="Verdana, Arial, Helvetica, sans-serif">#id1#</font></td>
    <td><font size="1" face="Verdana, Arial, Helvetica, sans-serif">#id2#</font></td>
    <td><font size="1" face="Verdana, Arial, Helvetica, sans-serif">#id3#</font></td>
      <td><font size="1" face="Verdana, Arial, Helvetica, sans-serif">#id4#</font></td>
      <td><font size="1" face="Verdana, Arial, Helvetica, sans-serif">#id5#</font></td>
    <td> <font size="1" face="Verdana, Arial, Helvetica, sans-serif">#id6#</font></td>
      <td><font size="1" face="Verdana, Arial, Helvetica, sans-serif">#id7# </font></td>
      <td> <font size="1" face="Verdana, Arial, Helvetica, sans-serif"><a href="#website#" target="_blank">#id8#</a></font></td>
      
      
 </tr>

</CFLOOP>
</cfoutput>
</table>



 
0
ostashenp
Asked:
ostashenp
  • 9
  • 7
1 Solution
 
gdemariaCommented:

 What you really want to show us is a sample of your data.  It seems that you don't have enough values in your list.  

 Keep in mind that coldfusion ignores blank elements on a list.  This is a big problem because of this...

Green|Blue||Red|Black  (notice the blank between red and blue)

#listgetAt(theList,4,"|")#

The answer is Black,  not Red as you would hope.
Because CF ignores the blank element.

The work around is to prepare your list by replacing blanks with some key to indicate that it's empty..

<cfset myList = replace(myList,"||","|NULL|","all")>

You may have to run this twice to ensure getting multiple empty elements in a row, such as   Red||||Green


0
 
ostashenpAuthor Commented:
As far as I know there are no blanks and what is weird is that all of the data from the file displays correctly, the error shows at the very bottom of the page.  If you give me your email I will send you the data its not something I really want to post.
0
 
ostashenpAuthor Commented:
Even if I delete fields 2-8 or 3-8 I get the error on the field that is left.  The only field I wont get an error on is field 1.
0
[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

 
ostashenpAuthor Commented:
Another thing to add to this whole problem is:

I am currently taking the text file which is located at a URL over http and manually saving to it a directory on my server.  Ideally I would like to have this script go and read directly from the URL and eliminate my having to save it to my server.
0
 
gdemariaCommented:

>  Even if I delete fields 2-8 or 3-8 I get the error on the field that is left.  The only field I wont get an error on is field 1.

What are the contents of the first field?   Are there any delimiters?  

It seems like you have the wrong delimiter, or for some reason its not matching.  The list seems to think there is only one item on the list.

>As far as I know there are no blanks and what is weird is that all of the data from the file displays correctly, the error shows at the very bottom of the page

Not sure what you mean by this.  What code is at the bottom of the page?  Is the code you're showing the code that works on doesn't work?

>  I am currently taking the text file which is located at a URL over http and manually saving to it a directory on my server.  Ideally I would like to have this script go and read directly from the URL and eliminate my having to save it to my server

CFHTTP  will read the contents into a variable  "cfhttp.fileContent"
You don't have to write it to a file.   But you can't read a file on another server without pulling it over..
0
 
ostashenpAuthor Commented:
>As far as I know there are no blanks and what is weird is that all of the data from the file displays correctly, the error shows at the very bottom of the page

Not sure what you mean by this.  What code is at the bottom of the page?  Is the code you're showing the code that works on doesn't work?


What I mean by this is, all of my data from the text file outputs in a table like i wanted.  All of the data from the file is there none of it is missing.....
The Cold fusion error shows up at the very bottom after all of the data is outputted.  If all of the data outputs why is it giving me this error ?
0
 
gdemariaCommented:

 that is a good question.  So even the very last line is printing?   The error should stop processing so whatever line throws the error is the last line shown.  

(You can also put a "THE END" statement at the very end of the file to see if actually get there.)

If you see the last line on the page, then perhaps there's a trailing extra blank line at the bottom of the file that's causing it?

Perhaps test the line using a CFIF to see if there is anything in the line being processed



0
 
ostashenpAuthor Commented:
Yes I have verified that it gets all the way to the end, and there is nothing special about how the last line ends its the same ending as every other line "~".  It does seem like it is trying to read in another line for some reason but it does not exist.  This file is just a plain text file its nothing fancy, This is also one of my first cold fusion scripts so I might need more detailed explanations as I am not totally familiar with the syntax yet.   I am not sure how I can stop this script from running once it hits the last line.
0
 
gdemariaCommented:

How about throwing this in your code right after the CFLOOP to see what happens...


<CFLOOP INDEX="orec" LIST="#datavar#" DELIMITERS="#crlf#">
  <cfif listLen(orec,"|") lt 7>
     <hr> This record failed - <br>
     [#orec#]<br>
     The length of this record is #listLen(orec,"|")#<br>
    <cfabort>
  </cfif>

   ... the rest of the code ...

0
 
ostashenpAuthor Commented:
Ok we are getting somewhere.

Now, it outputs the entire file, but now the error does not show up but at the top of the page this is displayed:

This record failed -
[ ]
The length of this record is 1

0
 
gdemariaCommented:

So the real data record of the file must end with a carriage return.    So coldfusion assumes there's another line after it to process, although that line is blank.

You can expand the CFIF to wrap the entire rest of your CFLOOP, so it won't process any blank lines...

<CFLOOP INDEX="orec" LIST="#datavar#" DELIMITERS="#crlf#">
  <cfif length(trim(orec)) >

    ... all your code here...

  </cfif>
</CFLOOP>
0
 
ostashenpAuthor Commented:
So close yet so far....
Not sure why this is happening ?

Variable LENGTH is undefined.
 
The error occurred in  line 35

33 :
34 : <CFLOOP INDEX="orec" LIST="#datavar#" DELIMITERS="#crlf#">
35 :   <cfif length(trim(orec)) >
36 :
37 :

Rest of my code is here

</cfif>
</cfloop>
0
 
gdemariaCommented:

 oops, sorry.  The function is len( )    not   length(   )

0
 
ostashenpAuthor Commented:
THANKS YOU !!!
0
 
ostashenpAuthor Commented:
How hard would it be to change the code I have which reads the data from a text file on my server and have it read that same data from a text file on a remote server via http ?
0
 
gdemariaCommented:

It would be very easy...

<cfhttp  url = "http://www.google.com"  throwOnError = "Yes">
</cfhttp>

<cfset datavar = cfhttp.fileContent>


Just replace the google URL with yours.  I've assigned the URL contents into your "datavar" variable.   There is no file to save/read.

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 9
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now