Fastest way to delete non-contiguous lines in list field

Jan Schenkel janschenkel at yahoo.com
Tue Jul 1 04:26:00 EDT 2003


--- Richard Gaskin <ambassador at fourthworld.com> wrote:
> Jan Schenkel wrote:
> 
> >> Hi all,
> >> 
> >> What's the fastest way to delete non-contiguous
> >> lines in a list field? These
> >> lines would be listed in the "hilitedLines".
> ...
> > Your problem would be that you have to either keep
> > track of the lines you have already deleted, or go
> > from the bottom to the top to avoid this but then
> you
> > can't use a 'repeat for each' construct, unless...
> > Try the following :
> > 
> > put the text of fld "Foobar" into tText
> > put the hilitedLines of fld "Foobar" into
> tLineNumbers
> > sort items of tLineNumbers numeric descending
> > repeat for each item tLineNumber in tLineNumbers
> > delete line tLineNumber of tText
> > end repeat
> > set the text of field "Foobar" to tText
> 
> I think "sort" may be an expensive operation:
> 
> on mouseUp
>   put 10000 into n -- iterations
>   put fld 1 into tOrigList
>   put the hilitedLines of fld 1 into tSelLines
>   --
>   -- Test 1:
>   put the millisecs into t
>   repeat n
>     --
>     put tOrigList into tList
>     sort items of tSelLines numeric descending
>     repeat for each item tLine in tSelLines
>       delete line tLine of tList
>     end repeat
>     --
>   end repeat
>   put the millisecs - t into t1
>   --
>   -- Test 2:
>   put the millisecs into t
>   repeat n
>     --
>     put tOrigList into tList
>     repeat for each item tLine in tSelLines
>       put "ARBITRARY_DELETE_FLAG" into line tLine of
> tList
>     end repeat
>     replace "ARBITRARY_DELETE_FLAG"&cr with empty in
> tList
>     --
>   end repeat
>   put the millisecs - t into t2
>   --
>   put t1 && t2  &&round( (t2/t1)*100) &"%"
> end mouseUp
> 
> I don't know why the latter is as quick as it is,
> but here it seems about
> 40% faster than the one relying on "sort".
> 
> -- 
>  Richard Gaskin 
> 

*sniffles* that's the second time in as many days that
'repeat for each' has betrayed me. *sobs*

By the way, the second script won't work correctly if
the user also selected the last line of the list
*grin*

Out of curiosity, how fast is this 'obvious' script in
comparison ?

    repeat for each line tLine in tOrigList
      add 1 to i
      if i is not among the items of tSelLines
      then put tLine & return after tList
    end repeat
    delete char -1 of tList

Jan Schenkel.

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

__________________________________
Do you Yahoo!?
SBC Yahoo! DSL - Now only $29.95 per month!
http://sbc.yahoo.com



More information about the use-livecode mailing list