Mystery with arrays (lost key)

Phil Davis phildavis at attbi.com
Tue Apr 9 23:18:01 EDT 2002


----- Original Message ----- 
From: "andu" <undo at cloud9.net>
To: <metacard at lists.runrev.com>
Sent: Tuesday, April 09, 2002 4:11 PM
Subject: Re: Mystery with arrays (lost key)


> Phil Davis wrote:
> > 
> > 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!
> 
> While you are right, the logic is still confusing:
> 
> on mouseUp
>   IF 1=1 THEN
>     put  1 into test2
> put 55 into  GrandTotals[test1]
>     put 66 into GrandTotals[test2]
>     put 22 into  GrandTotals[test3]
>   else
>     put 55 into  GrandTotals[test1]
>     put 66 into GrandTotals[test2]
>     put 22 into  GrandTotals[test3]
>   end if
>   
>   put keys (GrandTotals)
>   
>   -- returns
>   --
>   1
>   test1
>   test3
>   -
> end mouseUp
> 
> In this case test2 is still a variable but it also has a value and
> keys() returns the correct number of keys. Having to quote or not a
> variable name depending on it having a value is not exactly the Metacard
> I use to know ;-).


For me, the use of non-quoted literals is more the source of confusion than any other factor. If they weren't allowed, then MC would require the above lines in question to look like this:
>     put  1 into test2
>     put 55 into  GrandTotals["test1"]
>     put 66 into GrandTotals[test2]
>     put 22 into  GrandTotals["test3"]

Then it would be explicit and clear why keys(GrandTotals) are what they are above:
>   1        -- (value of 'test2' variable, used as a key)
>   test1    -- ("test1" string)
>   test3    -- ("test3" string)

I believe MC's assumption about 'test1' and 'test3' being char strings was meant to make life easier for us. In this case, however, MC's ease-of-use string-handling seems to be giving us enough rope to hang ourselves!

Phil
> 
> > 
> > Phil
> > 
> > _______________________________________________
> > metacard mailing list
> > metacard at lists.runrev.com
> > http://lists.runrev.com/mailman/listinfo/metacard
> 
> -- 
> ____________________
> Regards, Andu Novac
> _______________________________________________
> metacard mailing list
> metacard at lists.runrev.com
> http://lists.runrev.com/mailman/listinfo/metacard




More information about the metacard mailing list