Arrays in LC [was: Re: [TAO] value() to obtain pointer functionality]
Richard Gaskin
ambassador at fourthworld.com
Wed Aug 20 12:50:00 EDT 2014
Peter Haworth wrote:
> I may be misunderstanding but does that imply it's still possible to
> create "old style" arrays in Livecode? As a relative newcomer to LC,
> I've only ever known about the current form of arrays and wouldn't
> know how to set about creating one of the original arrays.
An array is just a collection of key-value pairs, and with associative
arrays each key is a string.
All that's changed in recent years is that -- in addition to any text or
binary data as we've always had -- you can now use an array as the value
for an element, effectively nesting them.
But for keys at any level, a string is a string, whether it's:
get tArray["Bob"]
...or:
get tArray["1"]
...or:
get tArray[1]
...or:
get tArray["1,2"]
...or:
put 1 into tFirstPart
put 2 into tSecondPart
get tArray[ tFirstPart, tSecondPart]
If you need to work on an array that's stored as the value in another
array, you'll want to use:
put tArray[1] into tAnotherArray
get tAnotherArray[2]
...or shorter, you can access the second-level array without extracting
it first by using a second set of brackets:
get tArray[1][2]
But with many uses, such as matrix multiplication, you'll probably never
need to be able to treat the second dimension as an extractable subset,
more interested in the collection as a whole.
So when working on one collection you can use:
get tArray[1,2]
We could illustrate each like this:
One-dimensional
tArray keys:
1,1
1,2
1,3
2,1
2,2
2,3
Two-dimensional
tArray keys:
1
tArray[1] keys:
1
2
3
2
tArray[2] keys:
1
2
3
Multidimensional arrays are very useful when the second (and any
subsequent dimensions) may be needed to be worked on as collections in
themselves.
But when only working on one collection as a whole, it probably doesn't
matter much whether you organize your collection as a one-dimensional
array in which parts of the key string provide a sort of row-and-column
designation, or make a two-dimensional array.
One advantage of the former is that it allows you to use the existing
matrixMultiply. :)
There may also be a modest performance gain from using a
single-dimensional array when practical, because each dimension has to
run the key through a hash function to determine the memory location of
its associated value. That said, the hashes used for most associative
aren't nearly as complex as cryptographic hashes, so I doubt the
performance difference would matter much.
--
Richard Gaskin
Fourth World Systems
Software Design and Development for the Desktop, Mobile, and the Web
____________________________________________________________________
Ambassador at FourthWorld.com http://www.FourthWorld.com
More information about the use-livecode
mailing list