Deleting lines within a 'repeat for each' loop
Martin Steer
martin at pixelmedia.com.au
Tue Aug 19 03:07:00 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
-----
The first iteration, the currLine variable gets ".ds_store", and then
it deletes line 1. All is okay.
The second iteration, the currLine variable gets the sting "1.xml"...
not "Help File 1.xml" as I assumed.
I tried removing the spaces in the file name thinking that may be
stuffing up the chunks, but the second iteration results in currLine
being set to the string "xml"
If you comment out the line that deletes the line from fileList, the
repeat loop works fine.
I know the algorithm is flawed i.e. when you delete line 1, increment i
to 2, then iterate again and delete line 2 it's actually deleting the
original line 3. This is my buggy test script :) That bug doesn't
matter though if each 'repeat' iteration the chunkType isn't correctly
being evaluated. Originally, the deletion of a line occurs when a line
matches the extension string at the end of the line (can't use
'contains' so I've been using matchText and regular expressions.)
So, my questions are these:
1) Regardless of weather there's an easier way to remove lines from a
string that don't match a pattern, is there something wrong with the
way the 'repeat for each' control structure evaluates chunks when you
manipulate the string it's repeating 'with'?
2) Is there an easier way to remove filenames from a list that don't
have specific file extensions?
Oh, I've tried in Rev 2.0.2 and 2.1 and I'm running on Mac OS X 10.2.6
Thanks for reading this far :)
- Marty Steer
martin at pixelmedia.com.au
More information about the use-livecode
mailing list