best way to reverse iterate through lines of a file?

Maybe there is a built in way to do this in Eggplant, but I want to find the text for the last LogError in my script’s log file.

I don’t know of a built in way to do this.
The Eggplant Reference shows a way to forward iterate through the Errors.
However, I think that it would be slightly more efficient, especially on LARGE log files, to reverse iterate the lines searching for “Error” and then exit repeat.

Is there a built in way to do what I want to do?
Is there a more efficient way to find the list error than:


set lastError to empty
set lastScriptResult to the last item of ScriptResults()
repeat with each line of file lastScriptResult.LogFile
  if the first word of it is "Error:" then set lastError to it
end repeat

Thanks!

Pv

There isn’t anything in SenseTalk right now that directly supports iterating over the lines in reverse order, so you’ll have to do it yourself. Here’s a modified script showing one idea for how to do this:

logError "first error"
logError "second error"
logError "this is the third error" -- give us something to search for
set lastError to empty 
set lastScriptResult to the last item of ScriptResults() 
put file lastScriptResult.LogFile into theLog
repeat with lineNum = the number of lines in theLog down to 1
	get line lineNum of theLog
	if it contains "LogError" then
		set lastError to it
		exit repeat
	end if
end repeat
replace every tab in lastError with " // "
log "Last Error: " & lastError

Note that I’ve read the log file into a variable and am iterating over that rather than the file directly. This saves some checking that SenseTalk will do otherwise on each line to see if the file may have been updated.

Depending on what you’re trying to get at, you might also look at using the offset() function, which can search a string backwards from the end.

Thanks! That is the same code that I came up w/ too.

Pv