Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CFFILE Read By Line

Status
Not open for further replies.

TechDude

Programmer
Jul 20, 2000
62
US
Hi all,
I have a csv file which I would like to read line by line into an array. The file is dynamically created so I cannot create an odbc source for it.
I would like to read it line by line into an array using cffile.
I'm guessing that I would need to read it into a variable using cffile and then loop through the string somehow...
any ideas?
Chris Sorel
chris@exnihilo.com
Remember, If you continue to do what you have always done,

you will continue to get what you have always gotten.
 
<cfloop index=&quot;line&quot; list=&quot;#FileContents#&quot; delimiters=&quot;#CHR(13)##CHR(10)#&quot;>
#line#<BR>
</cfloop>

HTH,
- Marc
 
hi techdude

You can loop through that variable you get with CFFILE using line breaks as delimiters, and then create an element in the array for every line. Then you can parse each line individually.

The IF is important to catch empty lines, and note that some operating systems write files differently, so #chr(10)# may also need #chr(13)# next to it as a delimiter. Test this.

<CFSET myfile =

'
apples,red
oranges,orange
lime,green
'>
<CFSET lineArray = arraynew(1)>
<CFSET incr = 0>
<CFLOOP list=&quot;#myfile#&quot; delimiters=&quot;#chr(10)#&quot; index=&quot;i&quot;><CFSET incr = incr+1>
<CFIF len(i) gt 1>
<CFOUTPUT>#incr#: #i# <br></CFOUTPUT>
<CFSET lineArray[incr] = i>
</CFIF>
</CFLOOP>

After you have your array of lines, you can just #listGetAt(lineArray[3], 2)# and thats the 2nd column for that 4th line (if CSV).

<CFOUTPUT>
#listGetAt(lineArray[3], 2)#
</CFOUTPUT>

let me know if this helped
 
That worked, much thanks to both of you.
Chris Sorel
chris@exnihilo.com
Remember, If you continue to do what you have always done,

you will continue to get what you have always gotten.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top