Mystery with arrays (lost key)

andu undo at cloud9.net
Tue Apr 9 21:20:01 EDT 2002


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 ;-).

> 
> Phil
> 
> _______________________________________________
> metacard mailing list
> metacard at lists.runrev.com
> http://lists.runrev.com/mailman/listinfo/metacard

-- 
____________________
Regards, Andu Novac



More information about the metacard mailing list