Coldfusion insert script from an external text file

Posted on 2007-04-03
Last Modified: 2013-12-24
Here is my problem and I cant seem to figure out why this is happening.  I had this all working but the person that maintains my text file made some changes and took out field number 8 so I had to modify my code:

Here is my code:


<CFSET crlf = "~">
<CFLOOP INDEX="orec" LIST="#datavar#" DELIMITERS="#crlf#">

<cfif len(trim(orec)) >

<cfquery name="getjobnumberInfo" datasource="TEST" dbtype="TEST">
      select max(jid) as NaxNum from Jobs

         Jobnumber = ListGetAt(orec, 1,"|");
         Jobtitle = ListGetAt(orec, 2,"|");
         Location = ListGetAt(orec, 3,"|");
         State = ListGetAt(orec, 4,"|");
         Company = ListGetAt(orec, 5,"|");
         Jobfield2 = ListGetAt(orec, 6,"|");
         Website = ListGetAt(orec, 7,"|");




The web site you are accessing has experienced an unexpected error.
Please contact the website administrator.

The following information is meant for the website developer for debugging purposes.  

Error Occurred While Processing Request  
Invalid list index 7.  
In function ListGetAt(list, index [, delimiters]), the value of index, 7, is not a valid as the first argument (this list has 6 elements). Valid indexes are in the range 1 through the number of elements in the list.  
The error occurred in D:\get_jobs.cfm: line 38
36 :          Company = ListGetAt(orec, 5,"|");
37 :          Jobfield2 = ListGetAt(orec, 6,"|");
38 :          Website = ListGetAt(orec, 7,"|");
39 : </CFSCRIPT>
40 :                  

Question by:ostashenp
  • 5
  • 3
  • 3
LVL 25

Expert Comment

ID: 18844513
obviously - somewhere in the text file there is a list that does not have 7 entries as delimited by |

so you need to catch this error so you can continue

replace your cfscript with:
<cftry><cfset Jobnumber = ListGetAt(orec, 1,"|")><cfcatch><cfset Jobnumber = ""></cfcatch></cftry>
<cftry><cfset Jobtitle = ListGetAt(orec, 2,"|")><cfcatch><cfset Jobtitle = ""></cfcatch></cftry>
<cftry><cfset Location = ListGetAt(orec, 3,"|")><cfcatch><cfset Location = ""></cfcatch></cftry>
<cftry><cfset State = ListGetAt(orec, 4,"|")><cfcatch><cfset State = ""></cfcatch></cftry>
<cftry><cfset Company = ListGetAt(orec, 5,"|")><cfcatch><cfset Company = ""></cfcatch></cftry>
<cftry><cfset Jobfield2 = ListGetAt(orec, 6,"|")><cfcatch><cfset Jobfield2 = ""></cfcatch></cftry>
<cftry><cfset Website = ListGetAt(orec, 7,"|")><cfcatch><cfset Website = ""></cfcatch></cftry>
LVL 39

Expert Comment

ID: 18845014

 Coldfusion ignore blanks in its lists.  So, if no entry is made in JobField2, the number of items on the list will be 6 not 7.  Therefore, your code will throw that error.

 The way around this is to preprocess every line to ensure there is a value for every item.   I do this by placing the word "NULL" between empty delimiters.  Like this:

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

Of course, when assigning into the variable, you have to take the extra step to look for NULL to get rid of it..

 <cfset Jobnumber = ListGetAt(orec, 1,"|")>
 <cfif  Jobnumber is "NULL">
     <cfset JobNumber  = "">

Yes, It's a pain!

 (You can replace "NULL" with anything that's unlikely to be in the datafile such as  $E#M#P#T#Y$   - use a variable to make it easier :)

LVL 39

Expert Comment

ID: 18845037

dgrafx,  I think your approach will allow the values to be misaligned.  If an element is missing in the 4th position, the 5th value will be assigned to the 4th element and so on..  The error won't occur until the 7th position.  I think your solution would make the 7th item empty while the other items would shift their values into the wrong variables.

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

LVL 25

Expert Comment

ID: 18845910
what you say is correct gdemaria

the bottom line though is that this text file is formatted incorrectly.
It's not just a matter of "the 8th element has been removed"
Your "text file maintainer guy" #$%&@ it up ...
LVL 39

Expert Comment

ID: 18846137

 Yes, it is entirely possible the text file is corrupt or not propperly structure.

 But don't count out the empty element, it would cause the error.  See this example..

 JOB87|President|Dallas|TX|ACME Corp||

 The above data record shows there is no value for JobField2 (the second to last element).  The record is a valid data record, however to coldfusion there are only 6 elements in it.  To Excel, for example, there are 7.   So the work-around I described would be needed to "see" all seven elements.
 JOB87|President|Dallas|TX|ACME Corp|NULL|

 Now there are 7 elements, the blank one is using a 'place holder'
LVL 25

Expert Comment

ID: 18846378
good point
i was going off of when poster said that it was all working fine when they had 8 fields and now reworked so there are only 7
so i then assumed that this particular problem was a formatting error

Author Comment

ID: 18875435
This whole project is a total nightmare.... Let me try the upload again today and will go from there.

Author Comment

ID: 18878111
Ok, how to I tell it to skip that line in the text file if all 7 elements are not there ?
LVL 39

Accepted Solution

gdemaria earned 500 total points
ID: 18878276

 Just wrap all your processing in this CFIF statement..

 <cfif listLen(orec,"|") eq 7>

 ......... process the record ...

LVL 39

Expert Comment

ID: 18878301

 can you open your text file and search for ||  (that's two pipes together)

 If you find them, you have empty values in your data and you need to run

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

 If you don't find them, then the CFIF will handle "bad" data records.

Author Comment

ID: 18882592
Thank you very much for your time and patience.

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
If I have to fix slow responding website my first thoughts are server side optimizations: the database may not be optimized or caching is not enabled, or things like that. We often overlook another major part of our web application: the client. We o…
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…
Learn how to set-up PayPal payment integration in your Wufoo form. Allow your users to remit payment through PayPal upon completion of your online form. This is helpful for collecting membership payments, customer payments, donations, and more.

777 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