put one array after another

Peter M. Brigham pmbrig at gmail.com
Thu Aug 11 11:57:37 EDT 2016


On Aug 11, 2016, at 11:37 AM, Matt Maier wrote:

> The way I've been tracking arrays in text for documentation purposes is
> basically just a table at heart:
> 
> array[first-key][this-key] = whatever
> array[first-key][that-key] = foobar
> array[2nd-key][some-key] = data
> array[2nd-key][another-one] = more data
> array[3rd-key][sub-key][new-level] = fake data
> array[3rd-key][sub-key][next-level] = probably also fake data
> array[3rd-key][sub-key][here-we-go-again] = totally the real data

This looks like the output of a function I use that was based on something Bob Sneider posted to this list.

function arrayToKeyList @pArray, _theKeyList
   -- from Bob Sneider, use-LC list, originally named altPrintKeys()
   --    adjusted by Peter M. Brigham, pmbrig at gmail.com
   -- returns a list of all the elements of pArray,
   --    with their keys, in the form of <key sequence> <tab> <element>, eg,
   -- [1] ["runs"] [1] ["style"] ["textColor"] 	0,0,0
   -- [1] ["runs"] [1] ["style"] ["textStyle"] 	bold
   -- [1] ["runs"] [1] ["text"] 	function
   -- [1] ["runs"] [2] ["style"] ["textColor"] 	0,0,0
   -- [1] ["runs"] [2] ["text"] 	 sr pStr
   -- [2] ["runs"] [1] ["style"] ["textColor"] 	0,0,0
   -- [2] ["runs"] [1] ["text"] 	   
   -- [2] ["runs"] [2] ["style"] ["textColor"] 	0,127,0  ...
   -- pArray is referenced to avoid duplication of large arrays in memory,
   --    but is not changed by this function
   -- _theKeyList is an internal parameter, used for the recursion
   --    do NOT pass a second parameter when calling this function
   --  keyListToArray() is the inverse function, allowing you to re-create the array
   --    from the key list
   -- requires deleteItem, getDelimiters(), keyListToArray()
   
   put numtochar(11) into vertTab
   put numtochar(30) into altCr
   put the keys of pArray into theKeys
   if line 1 of theKeys is a number then
      sort theKeys numeric
   else
      sort theKeys
   end if
   repeat for each line theKey in theKeys
      if theKey is a number then
         put "[" & theKey & "] " after _theKeyList
      else
         put "[" & quote & theKey & quote & "] " after _theKeyList
      end if
      if pArray[theKey] is an array then
         put pArray[theKey] into theTempArray
         put arrayToKeyList(theTempArray, _theKeyList) after theText
         deleteItem _theKeyList,-1,"] "
         put cr into char -1 of theText
      else
         if theKey is a number then
            put "pArray" && "[" & theKey & "]" into theKeyName
         else
            put "pArray" && "[" & quote & theKey & quote & "]" into theKeyName
         end if
         put value(theKeyName) into theValue
         replace tab with vertTab in theValue
         replace cr with altCr in theValue
         put _theKeyList & tab & theValue & cr after theText
         deleteItem _theKeyList,-1,"] "
      end if
   end repeat
   return theText
end arrayToKeyList

on deleteItem @pList, pItemNbr, pDelim
   -- deletes item pItemNbr of pList, given itemdel = pDelim
   -- based on replaceItem
   -- if pDelim = empty, defaults to tab as the delimiter
   -- pList is referenced, so the original list will be changed
   -- most useful when the itemdel is non-standard,
   --    don't have to set it then reset it in calling script
   -- also, pDelim can be a string of characters, so you could do
   --    pList = "first or ninth or second or third"
   --    deleteItem pList,2," or "
   --    and get "first or second or third"
   -- by Peter M. Brigham, pmbrig at gmail.com — freeware,
   --    based on a handler on the use-LC list
   -- requires getDelimiters()
   
   if pItemNbr = empty then exit deleteItem
   if pDelim = empty then put tab into pDelim
   if len(pDelim) > 1 then
      put getDelimiters(pList) into tempDel
      if tempDelim begins with "Error" then
         answer "Error in getDelimiters()" & cr & tempDelim
         exit to top
      end if
      replace pDelim with tempDel in pList
   else
      put pDelim into tempDel
   end if
   set the itemdelimiter to tempDel
   delete item pItemNbr of pList
   replace tempDel with pDelim in pList
end deleteItem

function getDelimiters pText, nbrNeeded
   -- returns a cr-delimited list of <nbrNeeded> characters
   --    none of which are found in the variable pText
   -- use for delimiters for, eg, parsing text files, manipulating 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 nbrNeeded = empty then put 1 into nbrNeeded -- 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 nbrNeeded > maxNbr then return "Error: max" && maxNbr && "delimiters."
   repeat for each item testCharNbr in baseList
      put numtochar(testCharNbr) into testChar
      if testChar is not in pText then
         -- found one, store and get next delim
         put testChar & cr after delimList
         if the number of lines of delimList = nbrNeeded
         then return line 1 to -1 of delimList
         -- done
      end if
   end repeat
   -- if we got this far, there was an error
   put the number of lines of delimList into totalFound
   if totalFound = 0 then
      return "Error: cannot get any delimiters."
   else if totalFound = 1 then
      return "Error: can only get 1 delimiter."
   else
      return "Error: can only get" && totalFound && "delimiters."
   end if
end getDelimiters

function keyListToArray theText
   -- the inverse function for arrayToKeyList()
   -- from Bob Sneider, use-LC list, originally named altKeysToArray()
   --    adjusted by Peter M. Brigham, pmbrig at gmail.com
   -- takes a list of all the elements of an array,
   --    with their keys, in the form of <key sequence> <tab> <element>,
   --    as created by arrayToKeyList(), eg,
   -- [1] ["runs"] [1] ["style"] ["textColor"] 	0,0,0
   -- [1] ["runs"] [1] ["style"] ["textStyle"] 	bold
   -- [1] ["runs"] [1] ["text"] 	function
   -- [1] ["runs"] [2] ["style"] ["textColor"] 	0,0,0
   -- [1] ["runs"] [2] ["text"] 	 sr pStr
   -- [2] ["runs"] [1] ["style"] ["textColor"] 	0,0,0
   -- [2] ["runs"] [1] ["text"] 	   
   -- [2] ["runs"] [2] ["style"] ["textColor"] 	0,127,0
   --    and loads it into an array
   -- requires deleteItem, getDelimiters(), arrayToKeyList()
   --    arrayToKeyList() is the inverse function
   
   put numtochar(11) into vertTab
   put numtochar(30) into altCr
   set the itemdel to tab
   repeat for each line theRecord in theText
      put item 1 of theRecord into theKeyList
      put item 2 of theRecord into theValue
      replace vertTab with tab in theValue
      replace altCr with cr in theValue
      put "put theValue into theArrayA" && theKeyList into theCommand
      do theCommand
   end repeat
   return theArrayA
end keyListToArray

----------

-- Peter

Peter M. Brigham
pmbrig at gmail.com





More information about the Use-livecode mailing list