Delete an Array Entry?

Richard Gaskin ambassador at fourthworld.com
Thu Mar 1 13:40:06 EST 2007


Dave wrote:

> On 1 Mar 2007, at 16:17, Richard Gaskin wrote: 
>> Dave wrote:
>>>>> delete global myArray[17]
>>> does not delete the 17th element, unless the key "17" just happens  
>>> to  the 17th element. The documentation is confusing on this  
>>> point. Also  the whole name "Array" for this datatype is  
>>> confusing, a better name  would be "table", since it doesn't  
>>> behave like an array at all.
>>
>> It behaves perfectly well as the associative array it is.
> 
> True, but address the 17th element makes no sense in this case, since  
> you are not actually address element 17, but rather the element whose  
> key is "17".

But the syntax is the same for both associative and numeric arrays:

   get MyArray[17]
   put "SomeData" into MyArray[17]

You can also access them with variables for the key:

   put 10+7 into tMyKey
   get MyArray[tMyKey]

So in most respects associative arrays provide the benefits of numeric 
arrays, but with the additional benefit of not restricting the index 
type to integers only.

If you need to address them in index order you can get that benefit of 
numeric arrays too by just sorting the keys:

   put the keys of MyArray into tKeys
   sort lines of tKeys
   repeat for each line tKey in tKeys
      get MyArray[tKey]
   end repeat

Sure, it's two extra lines for that specific usage, but for all the 
other flexibility of associative arrays I don't mind.

-- 
  Richard Gaskin
  Fourth World Media Corporation
  ___________________________________________________________
  Ambassador at FourthWorld.com       http://www.FourthWorld.com



More information about the use-livecode mailing list