0
votes

I have a text file in the format:

ABC | 123
DEF | 456

I've tried all sorts of things but can't get out what I need. What I need to be able to do is loop through each row and pick out (in the above example) ABC and 123 separately, as well as DEF and 456

<cfloop file="#application.sLibPath#301.txt" index="FileLine">
    <cfoutput>#listgetat(FileLine,1)#, #listgetat(FileLine,2)#</cfoutput><br>
</cfloop>

I've tried that but it doesn't work, i.e., it breaks. Anyone have any suggestions?

1
fyi - there are also listFirst and listLast functions that would work for you're doing. They allow you to pass in a delimiter just like listGetAt - Matt Busche
Just a note (@Miguel-F has given you the correct answer), but when you're posting a question involving an error, don't just say "there's an error", post the actual error. - Adam Cameron

1 Answers

7
votes

You are missing the delimiter portion of the ListGetAt function. By default the delimiter is a , (comma) but in your case you want a | character.

Try this:

<cfoutput>#listgetat(FileLine,1,"|")#, #listgetat(FileLine,2,"|")#</cfoutput><br>

Note: looking at your example data this might include some white space as well. Something like "ABC " and " 123" (notice the spaces). You can use the Trim() function to remove those.

<cfoutput>#Trim(listgetat(FileLine,1,"|"))#, #Trim(listgetat(FileLine,2,"|"))#</cfoutput><br>

Documentation for the ListGetAt function