Link to home
Start Free TrialLog in
Avatar of ostashenp
ostashenp

asked on

Coldfusion insert script from an external text file

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:

<CFFILE ACTION="READ" FILE="#Path#" VARIABLE="datavar">


<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
</cfquery>




<CFSCRIPT>
         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,"|");
</CFSCRIPT>

HERE IS THE FILE IT COMES FROM:

10000|POSITION|CITY|STATE|COMPANY|JOBFIELD2|URL ~


ERROR IM GETTING:

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 :                  

 
 
Avatar of dgrafx
dgrafx
Flag of United States of America image

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>

 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  = "">
 </cfif>

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 :)


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.

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

 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||www.google.com

 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|www.google.com

 Now there are 7 elements, the blank one is using a 'place holder'
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
Avatar of ostashenp
ostashenp

ASKER

This whole project is a total nightmare.... Let me try the upload again today and will go from there.
Ok, how to I tell it to skip that line in the text file if all 7 elements are not there ?
ASKER CERTIFIED SOLUTION
Avatar of gdemaria
gdemaria
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

 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.
Thank you very much for your time and patience.