Using numbers to access elements in arrays

Martin Baxter martin at harbourtown.co.uk
Thu Aug 7 15:46:00 EDT 2003


>>Devin Asay
>>
>>Correct me if I'm wrong...
>>
>>It seems to me that Rev's implementation of arrays is not typical as
>>compared to lower level languages. It seems to be more of a hash table
>>(like in Java). In other words, you store data in arrays by associating
>>each piece of data with a named key, rather than placing the data in
>>enumerated elements
>>

You're correct. They are associative not enumerated. You shouldn't rely on
the array keys being in any particular order.
Often they may be in ascii alphasort order as in :
1,10,11,12,13,14,15,16,17,18,19,2,20,21 but I don't know if this is
necesarily something to rely on.

Having said that, I have found that if you create an array using split,
from ordered lines of text, and then *do not* modify any element of the
array but only use it to look up values. It will retain its creation order.
This is a bit of a fragile state of affairs of course because the moment
you modify an element, that order is very likely going to be lost.

If you have to modify the data and also need to read the data out in
numeric order as if the keys were integers in sequence, you can:

put the keys of theArray into akeys
sort numeric lines of akeys
repeat for each line i in akeys
  put theArray[i] into something
end repeat

martin baxter






More information about the use-livecode mailing list