How can I read a txt file bottom up using cfloop? The below code reads it top to bottom.
<cfloop index="line" file="#myfile#">
<cfoutput>
The current line is #line# <br />
</cfoutput>
</cfloop>
s
Scott Steinbeck
05/12/2022, 11:18 PM
To my knowledge there is no way to start at the bottom. however https://docs.lucee.org/guides/cookbooks/loop_through_files.html provides a good guide for getting started, it is a more memory efficient way of getting to the bottom without loading the whole file in memory
👍 1
i
Iyob Zenebe
05/13/2022, 12:24 AM
I found a way. Read the file and put it in an array and then loop the array bottom up.
<cfloop index="i" from="#arrayLen(afilecontent)#" to="1" step="-1">
<cfoutput>#afilecontent[i]#</cfoutput><br>
</cfloop>
👍 1
d
Dave Merrill
05/13/2022, 12:57 PM
I figured, wrongly apparently, that you were tyring to avoid reading the whole file into memory at once. Without that restriction, what you've got should be fine.
i
Iyob Zenebe
05/13/2022, 12:59 PM
Thanks - the file size is relatively very small and that it why.
s
Scott Steinbeck
05/13/2022, 5:11 PM
I envisioned the same as @Dave Merrill since most times this question is asked about log files