which is faster for searching?

Peter Haworth pete at lcsql.com
Fri Aug 29 12:03:47 EDT 2014


I think it's worth pointing pout that the faster operation of repeat for
only applies if you're referencing Livecode chunks in the loop.  For
example, if you have a numerically keyed array, there's nothing wrong with:

put the extents of tArray into tExtents
repeat with x= item 1 of tExtents to item 2 of tExtents
  --do stuff with tArray[x]
end repeat

... as an alternative to:

put the keys of tArray into tKeys
sort tKeys numeric
repeat for each line rKey in tKeys
   --do stuff with tArray[rKey]
end repeat

There's another situation where I use repeat with even though it's a little
slower than repeat for and I also alter the contents of the data I'm
repeating through without any problems.

repeat with x=the number of lines in tVar down to to 1
   if <data condition on  line x of tVar> then
      delete line x of tVar
   end if
end repeat

I'm deleting some lines of tVar but because I start at the end of the
variable and work backwards, deleting a line doesn't affect whatever data
structures LC uses internally to keep track of the earlier lines.  You
could make changes to line x of tVar too and not have any problems

Pete
lcSQL Software <http://www.lcsql.com>
Home of lcStackBrowser <http://www.lcsql.com/lcstackbrowser.html> and
SQLiteAdmin <http://www.lcsql.com/sqliteadmin.html>


On Fri, Aug 29, 2014 at 2:58 AM, Beat Cornaz <B.Cornaz at gmx.net> wrote:

> Bob Sneidar wrote :
>
> > T he reason for all this is that to optimize the repeat loop, the engine
> makes one pass through the data, and creates an index of pointers to the
> delimiters. If you modify the data, the OS may (and probably will) do some
> memory shuffling > and the pointers will no longer be valid. What you
> return can be bits and pieces of the original data scrambled like eggs, or
> random bits of what is now in that memory space. Ick!
> >
> > ex. repeat for each line pLine in pData. Don?t alter pLine or pData
> inside the repeat control structure.
>
> I am on digest list form, so there probably have been people before me to
> address this. To be sure, here goes :
>
> I think the first part of what Bob says is true, you cannot alter the
> original data. But as you go into each turn of the loop and take rLine
> (see) below as a chunck, you can do with rLine whatever you like. I do that
> all the time. Only if you change the original data ( e.g. tPermutations)
> then the thing fouls.
>
> As an example :
>
> on mouseUp
> put empty into Changed
>
>    repeat for each line rLine in tPermutations
>       put 6 into item 3 of rLine      -- ** here I change rLine, which
> does not affect tPermutations
>       put rLine & cr after Changed
>    end repeat
>
> end mouseUp
>
> Beat Cornaz
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode
>



More information about the use-livecode mailing list