- The local variable which type is array, sometime it act like a global variable, for example:
:local testArr {1;2;3}
:log info "step1:testArr[0]=$($testArr->0)"
:set ($testArr->0) 11
:log info "step2:testArr[0]=$($testArr->0)"
when execute it for the first time, the output was:
step1:testArr[0]=1
step2:testArr[0]=11
from the second time, the output was:
step1:testArr[0]=11
step2:testArr[0]=11
So it seems that the local variable of testArr kept the old value and not reinit from the second time.
2.If the array was defined in function and as a return value of this function, then the same array value will be held by multi place. for example:
func1:
:global func1 do={
:global func3
:global func2
:local arr1 [$func3]
:log info "in func1: before execute func2:$($arr1->0)"
$func2
:log info "in func1: after execute func2:$($arr1->0)"
}
func2:
:global func2 do={
:global func3
:local arr2 [$func3]
:set ($arr2->0) 20
}
func3:
:global func3 do={
:local arra {8;4;5}
:log info "in func3: $($arra->0)"
return $arra
}
then execute func1 with this code:
:global func1
$func1
and the output was:
in func3: 8
in func1: before execute func2:8
in func3: 8
in func1: after execute func2:20
It means that the variable of arr2 in func2 and arr1 in func1 hold the same array variable, and they will affect each other.
Since i defined the array as local variable, these behaviors was not i want.
But these problems only occur when define the array with constants. for example
:local arr {1;2;3}
or
:local arr ({})
if define the array with:
:local arr {$a1;$b1;$c1}
or
:local arr [:toarry ""]
:set ($arr->0) 1
:set ($arr->1) 2
:set ($arr->2) 3
then it’s no problem.