Solved

ColdFusion data output for a text file

Posted on 2007-03-25
16
282 Views
Last Modified: 2008-02-01
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
Comment
Question by:ostashenp
  • 9
  • 7
16 Comments
 
LVL 39

Expert Comment

by:gdemaria
ID: 18790142

 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
 
LVL 2

Author Comment

by:ostashenp
ID: 18790151
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
 
LVL 2

Author Comment

by:ostashenp
ID: 18790166
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
 
LVL 2

Author Comment

by:ostashenp
ID: 18790201
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
 
LVL 39

Expert Comment

by:gdemaria
ID: 18790303

>  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
 
LVL 2

Author Comment

by:ostashenp
ID: 18790400
>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
 
LVL 39

Expert Comment

by:gdemaria
ID: 18790464

 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
 
LVL 2

Author Comment

by:ostashenp
ID: 18790470
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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 39

Expert Comment

by:gdemaria
ID: 18790500

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
 
LVL 2

Author Comment

by:ostashenp
ID: 18790514
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
 
LVL 39

Accepted Solution

by:
gdemaria earned 500 total points
ID: 18790559

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
 
LVL 2

Author Comment

by:ostashenp
ID: 18790567
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
 
LVL 39

Expert Comment

by:gdemaria
ID: 18790601

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

0
 
LVL 2

Author Comment

by:ostashenp
ID: 18790606
THANKS YOU !!!
0
 
LVL 2

Author Comment

by:ostashenp
ID: 18790616
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
 
LVL 39

Expert Comment

by:gdemaria
ID: 18792408

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
VBA for Outlook Web App 5 235
WEB Service vs ??? 7 102
CFFILE upload help 98 113
Problem to go to Web page 2 52
New Relic: Our company recently started researching several products to figure out what were the best ways for us to increase our web page speed and to quickly identify performance problems that we may be having. One of the products we evaluated wa…
It’s a strangely common occurrence that when you send someone their login details for a system, they can’t get in. This article will help you understand why it happens, and what you can do about it.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to choose which pages of your form are visible to your users based on their inputs. The page rules feature provides you with an opportunity to create if:then statements for y…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now