Deleting lines within a 'repeat for each' loop

Robert Brenstein rjb at rz.uni-potsdam.de
Tue Aug 19 04:14:46 EDT 2003


>Hello There,
>
>Just a quick question to everyone here.
>
>I've been trying to remove filenames (lines) from the string that 
>the 'files' function returns. The basic algorithm I've been using is 
>below, but I think I've found a problem in the 'repeat for each 
>chunkType' control structure in Rev.
>
>Here's a little piece of code I was using to test. I've taken the 
>filename checking out of the loop because that's not the problem. 
>Just put this in a button's script and step through it using the 
>variable watcher...
>
>-----
>on mouseUp
>
>   put ".ds_store" & return & "Help File 1.xml" & return & "Help File 
>3.xml" into fileList
>
>   put 1 into i
>
>   breakpoint
>   repeat for each line currLine in fileList
>     delete line i of fileList
>     add 1 to i
>   end repeat
>
>end mouseUp
>-----

Martin, your problem is that each delete shifts the content and the 
continued search skips some lines. What you simply need to do is to 
reverse the order. I mean you scan the list of files from the end to 
the beginning, so deletions do not affect your algorithm.

repeat with i=(the number of lines of filelist) down to 1

yes, this form of repeat is slower than repeat for but normally your 
list of files won't have thousands of lines, so it will do fine. If 
the speed is really an issue, then use the repeat for as you have it 
but instead of actually deleting lines, simply collect line numbers 
to be deleted and then delete them in another loop which uses a 
repeat down to 1.

Robert



More information about the use-livecode mailing list