Reverse a list

Peter M. Brigham pmbrig at gmail.com
Mon Feb 16 15:36:13 EST 2015


On Feb 16, 2015, at 1:58 PM, BNig wrote:

> Hi Peter,
> 
> you also might want to check your reverse algorithm on 7.x.x

Well, I'm still running 5.5.1, since I have a more than full-time job already taking care of patients and I don't have time to debug 38,000 lines of script for my practice management stacks on 6.5, let alone on 7.x. (I know, in theory, there should be backwards compatibility, but as Yogi Berra said, "In theory, theory and practice are the same, but in practice they're different." 6.5 breaks my stacks and I haven't figured out how.). I'll take your word for the virtues of 7.x re speed. At some point I'll have to upgrade -- I know I'm missing a lot of new (and not so new) developments now. My consolation is that 5.5 continues to work with no bugs for me at all.

> in my testing Jacque's initial post with little tweaks is as fast as your
> code and faster on 7.x.x (tested on 7.0.2 rc2) In my testing it took only
> 60% of the time compared to yours on 7.x.x
> Of course Alex Tweedly's ingenious inverse code leaves everything in the
> dust, except on 7.x.x.
> ----------------------------------------------------------------------
> on mouseUp
>   put field 1 into tData
>   put the milliseconds into t
>   get reverseText (tData)
>   put the milliseconds - t into field "fRes"
>   put it into field 1
> end mouseUp
> 
> local sNum
> function reverseText @pList
>   put the number of lines in pList into sNum
>   sort lines of pList numeric by reverseSort(each)
>   return pList
> end reverseText
> 
> private function reverseSort 
>   subtract 1 from sNum
>   return sNum
> end reverseSort

Outstanding, thanks Alex, and Berndt. Here's the slightly generalized version of Alex's code I've put in my utility stack. Since we're referencing pList, there doesn't seem to be a need to do this as a function. I changed the local variable name to avoid namespace conflicts in larger scripts. And my convention is to name all private utility functions using an underscore as the first char.

local reverseSortNbr
on reverseSort @pList, pDelim
   -- reverse sorts an arbitrary list
   -- pList is referenced, so the original list will be changed
   --    ie, item/line -1 -> item/line 1, item/line -2 -> item/line 2, etc.
   -- pDelim defaults to cr
   -- must declare a script local reverseSortNbr outside the handler
   -- from an exchange on the use-LC list
   --    this was the fastest pure-LC method of several proposed
   -- referencing pList and using a private sorting function
   --    speeds things up on large lists
   -- requires _decrCount()
   --    which is a private function

   if pDelim = empty then put cr into pDelim
   set the linedel to pDelim
   put the number of lines in pList into reverseSortNbr
   sort lines of pList numeric by _decrCount(each)
end reverseSort

private function _decrCount
   subtract 1 from reverseSortNbr
   return reverseSortNbr
end _decrCount

-- Peter

Peter M. Brigham
pmbrig at gmail.com
http://home.comcast.net/~pmbrig





More information about the use-livecode mailing list