Array key paths
Richard Gaskin
ambassador at fourthworld.com
Fri Nov 22 14:24:47 EST 2013
Alex Tweedly wrote:
> But Richard, in this example 's' is a numerically indexed array unless
> I'm misunderstanding).
>
> on mouseUp
> global gA
> put fld "key" into s
>
> ** so now variable 's' has the content "bob,age"
> split s by comma
> ** now 's' is an array, with two elements [1] == "bob, and [2] == "age"
> ** i.e. it is numerically indexed
> put gA[s] into fld "r"
> put the result
> end mouseUp
>
> I think it's confusing (for me at least) because we're using the split
> command to populate the index array - clearer if you do it directly
>
> This, in the msg box, works OK
> put "44" into gArray["bob"]["age"]
> put "bob" into tA[1]
> put "age" into tA[2]
> put gArray[tA] after msg
> and outputs "44"
>
> but if you modify it so the index array (tA) has an element with a
> non-numeric key, like
> put "44" into gArray["bob"]["age"]
> put "bob" into tA[1]
> put "age" into tA[2]
> put gArray[tA] after msg
> put "fail" into tA["two"]
> put gArray[tA] after msg
>
> then it fails with
> Message execution error:
> Error description: Array: bad index expression
> Hint:
Thanks, Alex. Yes, I was so excited to have exactly what I'd been
looking for in terms of being able to turn a simple string unto a key
path that I'd completely overlooked what's going on under the hood.
Good explanation.
> btw - you also say
>
> This is what I would expect, since associative arrays usually have no
> way to distinguish between numeric and non-numeric keys, since all of
> them are treated as strings when they go through the hash to assign
> their buckets.
>
> But it said in the release notes (for some version, no idea which) that
> there was an optimization in place for arrays for which the keys are a
> small number of consecutive, numeric values (i.e. I think, 1,2,3,....)
> so I guess the engine does keep track of whether or not an array's keys
> conform to this restriction.
That's very useful info - thanks. I missed that when it premiered - do
you know any other details about how to take advantage of it, or what
the optimization is doing under the hood?
If anyone here can recall which version of the Release Notes that's in
I'll be sure to add them to the archive at LiveCodeJournal.com:
<http://livecodejournal.com/features/release-notes.html>
--
Richard Gaskin
Fourth World
LiveCode training and consulting: http://www.fourthworld.com
Webzine for LiveCode developers: http://www.LiveCodeJournal.com
Follow me on Twitter: http://twitter.com/FourthWorldSys
More information about the use-livecode
mailing list