Building array from list

Ken Ray kray at sonsothunder.com
Mon Nov 1 09:06:48 EDT 2010


If you want to factor the code out so it's more reusable but a bit more
code, you could do this:

repeat for each line tLine in fld 1
  appendArray tDataA,(item 2 of tLine),(item 1 of tLine)
end repeat

command appendArray @pArray,pKey,pValue
  if pArray[pKey] is empty then
    put pValue into pArray[pKey]
  else
    put "," & pValue after pArray[pKey]
  end if
end appendArray


If you *really* wanted to genericize 'appendArray', you could pass in an
optional delimiter:

command appendArray @pArray,pKey,pValue,pOpt_Delim
  if pOpt_Delim is empty then put "," into tDelim
  else put pOpt_Delim into tDelim
  if pArray[pKey] is empty then
    put pValue into pArray[pKey]
  else
    put tDelim & pValue after pArray[pKey]
  end if
end appendArray

Hope this helps,

Ken


On 11/1/10 7:55 AM, "Mike Bonner" <bonnmike at gmail.com> wrote:

> Can also do it as follows:
> 
>    repeat for each line tLine in field 1
>       put the number of items in tdataA[(item 2 of tLine)] + 1 into tItemNum
>       put item 1 of tLine into item tItemNum of tDataA[(item 2 of tLine)]
>    end repeat
> 
> Can most likely combine the 2 lines of the repeat into a 1 liner, but
> this way works fine.
> 
> On 11/1/10, Gregory Lypny <gregory.lypny at videotron.ca> wrote:
>> Hello William,
>> 
>> This will give you an array with four keys corresponding to [1] fruit, [2]
>> dances, [3] colours, and [4] animals.
>> 
>> repeat for each line thisLine in theList
>> put the first item of thisLine & comma after myArray[the second item of
>> thisLine]
>> end repeat
>> 
>> The contents of each has a trailing comma, which can be removed by following
>> with
>> 
>> repeat for each key thisKey in myArray
>> if the last character of myArray[thisKey] is comma \
>> then delete the last character of myArray[thisKey]
>> end repeat
>> 
>> 
>> Regards,
>> 
>> Gregory
>> 
>> 
>> On Mon, Nov 1, 2010, at 8:15 AM, use-revolution-request at lists.runrev.com
>> wrote:
>> 
>>> I was wondering. If you have a list:
>>> 
>>> apple,1
>>> orange,1
>>> grapefruit,1
>>> tango,2
>>> blue,3
>>> green,3
>>> yellow,3
>>> zebra,9
>>> 
>>> And you want to convert it into an array:
>>> 
>>> (apple,orange,grapefruit[1])
>>> (tango[2])
>>> (blue,green,yellow[3])
>>> (zebra,[9])
>>> 
>>> What would be the easiest way. I'm always a little confused by arrays but
>>> it
>>> seems to me this should be a very simple and fast conversion. The array is
>>> intentionally a string "apple,orange,grapefruit" that results from key [1]
>> 
>> _______________________________________________
>> use-revolution mailing list
>> use-revolution at lists.runrev.com
>> Please visit this url to subscribe, unsubscribe and manage your subscription
>> preferences:
>> http://lists.runrev.com/mailman/listinfo/use-revolution
>> 
> _______________________________________________
> use-revolution mailing list
> use-revolution at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription
> preferences:
> http://lists.runrev.com/mailman/listinfo/use-revolution

Ken Ray
Sons of Thunder Software, Inc.
Email: kray at sonsothunder.com
Web Site: http://www.sonsothunder.com/





More information about the use-livecode mailing list