Collapse (?) An Array?

Richard Gaskin ambassador at fourthworld.com
Thu Sep 1 11:37:35 CDT 2011


Scott Rossi wrote:

> Recently, Richard Gaskin wrote:
>
>> The delete is fine; the problem is in the display of the results.
>>
>> If you use:
>>
>>    put theArray[1] &"*"&theArray[2] &"*"& theArray[3]
>>
>> ...you'll get the empty entry for theArray[2].  The variable theArray
>> still exists, but there's nothing in that slot.
>
> Yes, this is the problem.  I'm storing data in array form in custom
> properties, and my problem is after a delete, the remaining elements of the
> array are not reordered.

True, the order of elements in an associative array is not maintained 
across split and combine, but within the array there is no "order" per 
se, just a set of slots that point to memory locations.

If order is important you'll want to use the sort command on the array 
keys and access them in that sorted order.

>> Building the output from the keys of the array will get what you want:
>>
>>    put the keys of theArray into tKeys
>>    sort lines of tKeys -- if the order is important
>>    repeat for each line tKey in tKeys
>>       put "*"& theArray[tKey] after tResult
>>    end repeat
>>    put tResult
>
>From what you're saying, it sounds like I need to put the remaining elements
> of the array into a temporary variable and then replace the original array.

Not at all.  The array's fine.  All you need to do is tailor your output 
according to your needs.

It may be helpful to remember that all arrays in LiveCode are 
associative arrays.  Any keys you use are strings, whether comprised of 
alphanumeric or strictly numeric characters.

The slots in an associative array aren't numbered per se, they're named.

Using the keys of the array will show you which names are in use, and 
they can be sorted to get any output you need.

--
  Richard Gaskin
  Fourth World
  LiveCode training and consulting: http://www.fourthworld.com
  Webzine for LiveCode developers: http://www.LiveCodeJournal.com
  LiveCode Journal blog: http://LiveCodejournal.com/blog.irv



More information about the use-livecode mailing list