Ex:
Code: Select all
:global VALUE true
:foreach varname in "VAR1\n\rVAR2\n\rVAR3" \
do={
:global "$varname" $VALUE
}
:global VALUE true
:foreach varname in "VAR1\n\rVAR2\n\rVAR3" \
do={
:global "$varname" $VALUE
}
:foreach varname in={"VAR1" ; "VAR2" ; "VAR3" } do={:parse ":global $varname \$VALUE" }
:foreach varname in={"VAR1" ; "VAR2" ; "VAR3" } do={:execute ":global $varname \$VALUE" }
I used $VALUE instead of \$VALUE, but it worked perfectly.Using ":execute" instead of ":parse", it works:
Code: Select all:foreach varname in={"VAR1" ; "VAR2" ; "VAR3" } do={:execute ":global $varname \$VALUE" }
Hi Tal,I used $VALUE instead of \$VALUE, but it worked perfectly.
It didn't seem to work for me when I used "\$VALUE", but I'll try again. Pretty useful bit of info - thanks.Hi Tal,I used $VALUE instead of \$VALUE, but it worked perfectly.
Without "\" it works for most values. But If VALUE contains, for example, a string with spaces or special characters, "$VALUE" won't work while "\$VALUE" will...![]()
BTW, a warning: I may have missed something but AFAIK, if the ":execute" f command fails, you get no error message of any kind.![]()
Fabrice
This code doesn't work. But it may give a hint toward a solution :
foreach varname in={"VAR1"; "VAR2"; "VAR3"} do={[parse "global $varname true"]}
Store the dynamically created local variables in an array.Works with global, but not possible with local. Is the any solution for local variables?
{
:local qty 9; # number of variables to create
:local varname "myvar"; # name for the variables
:local myarray [:toarray ""]; # create empty array
:for i from=1 to=$qty do={
:local tmp ($varname . [:tostr $i]); # dynamically construct name of the variable
:set ($myarray->$tmp) ""; # push the newly created variable into the array
}
# setting the variables
:set ($myarray->"myvar1") "plaintext"
:set ($myarray->"myvar2") {"abc"}
:set ($myarray->"myvar3")
:set ($myarray->"myvar4") true
:set ($myarray->"myvar5") 127.0.0.1
:set ($myarray->"myvar6") 23:59:59
:set ($myarray->"myvar7") 192.168.88.0/24
:set ($myarray->"myvar8") ::ffff:192.0.2.128
:set ($myarray->"myvar9") 2001:0DB8:0000:000b::/64
# reading the variables
:put ( "myvar1=" . ($myarray->"myvar1") . " type=" . [ :typeof ($myarray->"myvar1") ] )
:put ( "myvar2=" . ($myarray->"myvar2") . " type=" . [ :typeof ($myarray->"myvar2") ] )
:put ( "myvar3=" . ($myarray->"myvar3") . " type=" . [ :typeof ($myarray->"myvar3") ] )
:put ( "myvar4=" . ($myarray->"myvar4") . " type=" . [ :typeof ($myarray->"myvar4") ] )
:put ( "myvar5=" . ($myarray->"myvar5") . " type=" . [ :typeof ($myarray->"myvar5") ] )
:put ( "myvar6=" . ($myarray->"myvar6") . " type=" . [ :typeof ($myarray->"myvar6") ] )
:put ( "myvar7=" . ($myarray->"myvar7") . " type=" . [ :typeof ($myarray->"myvar7") ] )
:put ( "myvar8=" . ($myarray->"myvar8") . " type=" . [ :typeof ($myarray->"myvar8") ] )
:put ( "myvar9=" . ($myarray->"myvar9") . " type=" . [ :typeof ($myarray->"myvar9") ] )
}