Reverse a list

Dave Cragg dave.cragg at lacscentre.co.uk
Tue Feb 17 14:25:16 CET 2015


Alex,

Thanks for that.

Disbelieving soul that I am (sorry), I puzzled for a while over the results of these two versions. 

on mouseup
   put empty into msg
     put "abc" & CR & "def" & CR & "ghi" & CR  into t
     repeat for each line L in t
           put the number of chars in L && L &CR after msg
           if L = "abc" then
                 put "x" into line 2 of t
                 put "y" into line 3 of t
           end if
     end repeat
end mouseup

[no final CR on original t]
on mouseup
   put empty into msg
     put "abc" & CR & "def" & CR & "ghi"  into t
     repeat for each line L in t
           put the number of chars in L && L &CR after msg
           if L = "abc" then
                 put "x" into line 2 of t
                 put "y" into line 3 of t
           end if
     end repeat
end mouseup

I had to use a pencil and paper to track what was in t and what the engine was referring to after the "x" and "y" inserts. Then it became clear.

I feel a better person. 

Cheers
Dave



> On 16 Feb 2015, at 23:08, Alex Tweedly <alex at tweedly.net> wrote:
> That's not quite correct. It doesn't do a single initial complete scan of the whole variable and keep all the pointers. What it does is (more like) keep track of how far it has currently processed, and then when it needs the next line, it scans from that remembered position forward until the next CR - and then remembers that as the current position.  (Oh, and btw, it does remember how many chars were in the initial value, and uses that).
> 
> The fact that it does incremental scan rather than one initial scan can be seen from the following (totally useless :-) bit of code :
> 
> on mouseup
>   put "abc" & CR & "def" & CR & "ghi" & CR into t
>   repeat for each line L in t
>      put the number of chars in L && L &CR after msg
>      if L = "abc" then
>         put "x" into line 2 of t
>         put "qwert" after line 3 of t
>      end if
>   end repeat
> end mouseup
> 
> An initial scan would have produced  :
> 3 abc
> 3 x
> g
> 3 ijq
> 
> But we actually get:
> 3 abc
> 1 x
> 6 ghiqwe
> 
> It is significant that it doesn't do a complete scan, because that would be a cost penalty at the start of the loop, and so could make it unnecessarily expensive to do some operations. Again that can be seen from the following (take out of context) snippet of not quite real code ....
> 
> put "millions and millions of lines of data" into tVar
> repeat for each line L in tvar
>   if some condition then exit repeat
> end repeat
> 
> If there were an initial scan, then you would do that whole costly scan even though you could actually exit the loop very early; because it's an incremental scan, you don't need to worry about that (so long as you are sure you will exit soon).
> 
> -- Alex.




More information about the use-livecode mailing list