Deleting lines within a 'repeat for each' loop

Jan Schenkel janschenkel at yahoo.com
Tue Aug 19 03:27:02 EDT 2003


--- Martin Steer <martin at pixelmedia.com.au> wrote:
> 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
> 

Hi Marty,

The reason why you are getting such 'strange' results
is that in a repeat for each loop, the engine
remembers where the current line ended, and that's
where it starts looking for the next lineDelimiter.
So in this case, it finds the first line delimiter at
position 9. Then you delete the first line, but the
next repeat loop starts looking at position 10 in the
changed variable. The next lineDelimiter is at new
position 16, so it thinks that line 2 stretches from
position 10 to 16 -- which explains why it gives you "
1.xml" the next time around.
Etcetera, etcetera.

As you shouldn't tinker with the variable you're
interating over, here are a few alternative approaches
to your goal :

1) check out the Transcript Dictionary entry for the
'filter' command.
  filter tFileList with "*.foo,*"

2) use a repeat for each, but build a new variable
with those lines that you _do_ want, and then put that
new variable into your first variable.
  repeat for each line tFileInfo in tFileList
    if you_want_this_line
    then put tFileInfo & return after tNewList
  end repeat
  put char 1 to -2 of tNewList into tFileList
  -- by using 1 to -2, strip off the trailing return
in variable tNewList

Hope this helped,

Jan Schenkel.

=====
"As we grow older, we grow both wiser and more foolish at the same time."  (La Rochefoucauld)

__________________________________
Do you Yahoo!?
Yahoo! SiteBuilder - Free, easy-to-use web site design software
http://sitebuilder.yahoo.com



More information about the use-livecode mailing list