Reverse a list

Peter M. Brigham pmbrig at gmail.com
Mon Feb 16 21:23:58 EST 2015


On Feb 16, 2015, at 9:05 PM, Dr. Hawkins wrote:

> On Mon, Feb 16, 2015 at 3:08 PM, 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.
> 
> 
> And if you repeat for each item, and change the itemDel in the loop, you
> had *better* remember to change it back before end repeat, or very strange
> things happen . . .

No need to change the itemdel in a loop, you can use this instead:

put getItem(pList, pIndex, pDelim) into tItem

-- Peter

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

-----

function getItem pList, pIndex, pDelim
   -- returns item # pIndex of pList, given itemdelimiter = pDelim
   -- could just "get item pIndex of pList" in the calling handler but
   --    then have to set and restore the itemDelimiter, so this is less hassle
   -- defaults to pDelim = tab
   -- allows pDelim to be a string of characters
   --    so you could do this:
   --    getItem("a//b//c//d//e//f",4,"//") -> d
   -- also allows pIndex to be a range, eg "3-5"
   --    in that case enclose the range in quotes
   -- by Peter M. Brigham, pmbrig at gmail.com — freeware
   
   -- requires getDelimiters()
   
   if pDelim = empty then put tab into pDelim
   put getDelimiters(pList) into tempDelim
   if tempDelim begins with "Error" then return "Error in getDelimiters()"
   replace pDelim with tempDelim in pList
   set the itemdelimiter to tempDelim
   put offset("-",pIndex) into dashPos
   if dashPos > 1 then
      -- don't catch if pIndex is something like -1, -2, etc
      put char 1 to dashPos-1 of pIndex into tStart
      put char dashPos+1 to -1 of pIndex into tEnd
      put item tStart to tEnd of pList into theItem
      replace tempDelim with pDelim in theItem
   else
      put item pIndex of pList into theItem
   end if
   return theItem
end getItem

function getDelimiters pText, nbr
   -- returns a cr-delimited list of <nbr> characters
   --    not found in the variable pText
   -- use for delimiters for, eg, parsing text files, loading arrays, etc.
   -- usage: put getDelimiters(pText,2) into tDelims
   --        if tDelims begins with "Error" then exit to top -- or whatever
   --        put line 1 of tDelims into lineDivider
   --        put line 2 of tDelims into itemDivider
   --             etc.
   -- by Peter M. Brigham, pmbrig at gmail.com — freeware
   
   if pText = empty then return "Error: no text specified."
   if nbr = empty then put 1 into nbr -- default 1 delimiter
   put "2,3,4,5,6,7,8,16,17,18,19,20,21,22,23,24,25,26" into baseList
   -- low ASCII values, excluding CR, LF, tab, etc.
   put the number of items of baseList into maxNbr
   if nbr > maxNbr then return "Error: max" && maxNbr && "delimiters."
   repeat with tCount = 1 to nbr
      put true into failed
      repeat with i = 1 to the number of items of baseList
         put item i of baseList into testNbr
         put numtochar(testNbr) into testChar
         if testChar is not in pText then
            -- found one, store and get next delim
            put false into failed
            put testChar into line tCount of delimList
            exit repeat
         end if
      end repeat
      if failed then
         if tCount = 0 then
            return "Error: cannot get any delimiters."
         else if tCount = 1 then
            return "Error: can only get one delimiter."
         else
            return "Error: can only get" && tCount && "delimiters."
         end if
      end if
      delete item i of baseList
   end repeat
   return delimList
end getDelimiters






More information about the use-livecode mailing list