Confuse about Global Array Variable

I have this following code that confuse me…

:global test ({});

:local a $test;

/log info $a;

:set ($test->"what") "yes it is";

/log info $a;

Output :

(blank/empty line)
what=yes it is

based on the output… why is it that $a also have its value changed when the only variable I changed was the $test variable..

next one was I deleted the test global variable on the environment tab… and run it again…
Output:

what=yes it is
what= yes it is

why is it that it outputs twice when I deleted the global variable and already have declared this at top :global test ({}); ← (which i assumed it will create a new empty array).. can anyone please help me or explain it to me..

I think when you set a variable equal to an array variable, it will not copy the array but it will
make the variable a pointer to the same array. Similar to what happens when you pass a function parameter.

I have done some script programming with array and indeed it sometimes behaves a bit weird when you
are used to some other languages. And worst, there are no useful error messages so you can have bad
code without knowing where the error is… especially when it is running from some system event.

What you said does make sense to me :smiley: .. I tried it vice versa

:global test ({});

:local a $test;

/log info $test;

:set ($a->"what") "yes it is";

/log info $test;

and it showed the same results… and yea… I am used to other programming language that’s why I find it very weird… the pointer thing did not cross my mind :smiley: … Thanks for the reply :slight_smile:

A trick I discovered by accident while pulling my hair out to find some bug:
When you define a global function like:
:global funcname do={

}

and then you display the global variables, you see a “parsed” version of the function in
a stack language, and you can sometimes see problems that you would miss otherwise.
(and it becomes clear why runtime error messages cannot reference the line and character where they occur)

Woah this is very handy!.. thnxs for this trick pe1chl :sunglasses: .