Array key paths

Alex Tweedly alex at tweedly.net
Fri Nov 22 14:17:35 EST 2013


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:


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.

-- Alex.

On 22/11/2013 17:38, Richard Gaskin wrote:
>
> This test seems to work:
>
> Script of one button to load the array:
>
> on mouseUp
>    global gA
>    put "bob" into gA["bob"]
>    put "44" into gA["bob"]["age"]
>    put "ted" into gA["ted"]
>    put "48" into gA["ted"]["age"]
> end mouseUp
>
> Script of a second button to split a key and use it to retrieve a value:
>
> on mouseUp
>    global gA
>    put fld "key" into s
>    split s by comma
>    put gA[s] into fld "r"
>    put the result
> end mouseUp
>
>
> There are two fields - the one named "key" has:
>
> bob,age
>
> ...and when I click the second button the field named "r" shows:
>
> 44
>
> Nothing shows in the Message Box, so presumably there was no error.
>
> 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.
>
> -- 
>  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
>
> _______________________________________________
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your 
> subscription preferences:
> http://lists.runrev.com/mailman/listinfo/use-livecode





More information about the use-livecode mailing list