Deleting 1 line from a long list
Cubist at aol.com
Cubist at aol.com
Tue Oct 21 05:27:37 EDT 2003
sez igor at pixelmedia.com.au:
>I was wondering if the way that I'm going about a certain task is
>actually the easiest/fastest way. The task is: I have a list
>(return-delimited) of several hundred words. I need to delete ONE
>specific word from the list.
>
>Right now, I'm using a repeat loop, iterating through the list until I
>find the string, then deleting it:
>
># PRE: pLIst should be a return-delimited list of strings. pString
>should be the text of one of the items in pList.
># POST: pList is returned, minus the line that contains/is pString.
>function fDeleteLine pList,pString
> if pList is empty then return empty
> if pString is empty then return pList
> repeat with x = 1 to number of lines in pList
> if line x of pList is pString then
> delete line x of pList
> exit repeat
> end repeat
> return pList
>end fDeleteLine
>
>I know that the "repeat with x" structure is supposed to be slower than
>other forms of repeat, and I also seem to remember some experienced
>users in this list recommending using arrays in some way to speed up
>search and replace functions in lists.
>
>So: is there a faster way to do this?
Don't bother with a repeat loop at all.
function fDeleteLine pList,pString
if pList is empty then return empty
if pString is empty then return pList
put offset ((return & pString & return),(return & pList & return)) into
Fred
if Fred = 0 then
# pString isn't in pList at all
return empty
else
put the number of lines in char 1 to Fred of pList into George
delete line George of pList
return pList
end if
end fDeleteLine
This is untested, off-the-top-of-my-head code, but it should illustrate
the idea...
More information about the use-livecode
mailing list