Mystery with arrays (lost key)

Phil Davis phildavis at attbi.com
Tue Apr 9 20:43:00 EDT 2002


There's nothing wrong with MC. Here's what's happening:

In the following example, 'test2' is first used as a variable name. It's never in the execution path, but that doesn't matter; MC still recognizes it as a variable. Later in the same handler, that same 'test2' variable is being used as an array key. This means that the value found in 'test2' - empty in this case - is the actual key value.

> on mouseUp
>   IF 1=2 THEN
>     put  1 into test2
>   else
>     put 55 into  GrandTotals[test1]
>     put 66 into GrandTotals[test2]
>     put 22 into  GrandTotals[test3]
>   end if
> 
>   put keys (GrandTotals)
> 
>   -- returnes
>   --
> 
>   test1
>   test3
>   -
> end mouseUp


If you want a key whose value is 'test2' while still having a variable named 'test2', you have to use quotes around the key value, like this:

>     put 55 into  GrandTotals[test1]
>     put 66 into GrandTotals["test2"]
>     put 22 into  GrandTotals[test3]

MC assumes the quotes around 'test1' and 'test3' and treats them as literals (char strings) in the above case because they're not used as variable names.

I hope this explanation isn't too opaque!

Phil





More information about the metacard mailing list