Tal
August 15, 2015, 2:45am
1
Is there any way to use dynamic names for variables?
Ex:
:global VALUE true
:foreach varname in "VAR1\n\rVAR2\n\rVAR3" \
do={
:global "$varname" $VALUE
}
I would think that the above code should work, but it doesn’t, because :global doesn’t like taking a variable name from a variable. Any way to get around this?
This code doesn’t work . But it may give a hint toward a solution :
:foreach varname in={"VAR1" ; "VAR2" ; "VAR3" } do={:parse ":global $varname \$VALUE" }
Hope it can help.
Using “:execute” instead of “:parse”, it works :
:foreach varname in={"VAR1" ; "VAR2" ; "VAR3" } do={:execute ":global $varname \$VALUE" }
Tal
August 28, 2015, 3:55am
4
favincen:
Using “:execute” instead of “:parse”, it works :
:foreach varname in={"VAR1" ; "VAR2" ; "VAR3" } do={:execute ":global $varname \$VALUE" }
I used $VALUE instead of $VALUE, but it worked perfectly.
Thanks guys.
Hi Tal,
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
Tal
August 28, 2015, 4:01pm
6
favincen:
Hi Tal,
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
It didn’t seem to work for me when I used “$VALUE”, but I’ll try again. Pretty useful bit of info - thanks.
Winand
August 4, 2018, 10:13am
7
You just need square brackets:
foreach varname in={"VAR1"; "VAR2"; "VAR3"} do={[parse "global $varname true"]}
yeah but this doesn’t work in this setting, if you put space in the values
foreach key,value in={"VAR1"="foo with space"; "VAR2"="bar"} do={[parse "global $key $value"]}
I hope that thread is still active as I have a similar challenge
[admin@MikroTik] > :global vlancommon {id=100; name="VLAN_COMMON" ; comment="COMMON SERVICES AND DEVICES / OFFICE" }
[admin@MikroTik] > :put ($vlancommon)
comment=COMMON SERVICES AND DEVICES / OFFICE;id=100;name=VLAN_COMMON
[admin@MikroTik] > :put ($vlancommon->"name")
vlancommon
[admin@MikroTik] > :put ([ :toarray "UP, vlancommon ,KIDS" ]->1)
vlancommon
[admin@MikroTik] > :put (:parse "\$([ :toarray "UP, vlancommon ,KIDS" ]->1)"->"name")
comment=COMMON SERVICES AND DEVICES / OFFICE;id=100;name=VLAN_COMMON
but I would expect
VLAN_COMMON
in the last line.
When I start moving around the quotes, the result is even weirder.
Any idea what the last command has to look like to get the second element of the array vlancommon ?
Last but not least, how to modify when I going to use capital later and/or symbols in the variable name and/or array key name so that they have to be put in quotes?
thx
stefan
could solve it:
had do add another pair of (), to enclose the parse command
:put ( ( :parse "\$([ :toarray "UP, vlancommon ,KIDS" ]->1)" ) -> "name")
.
and pair of "
:put ( ( :parse "\$\"([ :toarray "UP, vlancommon ,KIDS" ]->1)\"" )->"name")
Returning to OP question, is simple:
search tag # rextended dynamic variables
This:
:global $wan1 1;
:put ("wanSta" . $wan1);
:set ("wanStatus" . $wanIndex) 10
give two errors:
first one because the $ on front of wan1 is forbiden when declaring a global or local variable (I use 6.47.10)
second because the wan1 is undefined on second line for the reason writed before
third on last line you use another name wanStatus
The correct syntax is:
:global wan1 1
:put "wanSta$wan1"
:set wanSta1 10
but obviously do not create or s…
The soluction is:
:global variablename "test"
Create or set a value (on same command)
[:parse ":global $variablename "REX1""]
:put $test
Set a value (or simply apply previous command)
[:parse "global $variablename;:set $variablename "REX2""]
:put $test
For read the variable inside another declared variable:
:global testx
[:parse "global $variablename;:global testx $$variablename"]
:put $testx
I made an error here; did not test “vlan_common”.
Any idea how to make
:put ( ( :parse "\$\"([ :toarray "UP, vlan_common ,KIDS" ]->1)\"" )->"name")
work?
for example, start to remove all significant spaces:
:put ">$([:toarray "UP, vlancommon ,KIDS"]->1)<"
>vlancommon <
:put ">$([:toarray "UP,vlancommon,KIDS"]->1)<"
>vlancommon<
for example, start to remove all significant spaces:
:put ">$([:toarray "UP, vlancommon ,KIDS"]->1)<"
>vlancommon <
:put ">$([:toarray "UP,vlancommon,KIDS"]->1)<"
>vlancommon<
try the same but replace vlancommon by vlan**_**common
result shall be:
VLAN_COMMON
from
[admin@MikroTik] > :global "vlan_common" {id=100; name="VLAN_COMMON" ; comment="COMMON SERVICES AND DEVICES / OFFICE" }
whu replace “vlancommon by vlan_common”?
the problem are the spaces between elements on array…
and parse do not give results, you must USE parse…
:global vlancommon {id=100;name="VLAN_COMMON";comment="COMMON SERVICES AND DEVICES / OFFICE"}
:put ($vlancommon)
:put ($vlancommon->"name")
:put ([:toarray "UP,vlancommon,KIDS"]->1)
:global testx
[:parse "global vlancommon;:global testx (\$$([:toarray "UP,vlancommon,KIDS"]->1)->\"id\")"]
:put $testx
[:parse "global vlancommon;:global testx (\$$([:toarray "UP,vlancommon,KIDS"]->1)->\"name\")"]
:put $testx
[:parse "global vlancommon;:global testx (\$$([:toarray "UP,vlancommon,KIDS"]->1)->\"comment\")"]
:put $testx
[mikro@tik] > :global vlancommon {id=100;name=“VLAN_COMMON”;comment=“COMMON SERVICES AND DEVICES / OFFICE”}
[mikro@tik] > :put ($vlancommon)
comment=COMMON SERVICES AND DEVICES / OFFICE;id=100;name=VLAN_COMMON
[mikro@tik] > :put ($vlancommon->“name”)
VLAN_COMMON
[mikro@tik] > :put ([:toarray “UP,vlancommon,KIDS”]->1)
vlancommon
[mikro@tik] > :global testx
[mikro@tik] > [:parse “global vlancommon;:global testx ($$([:toarray “UP,vlancommon,KIDS”]->1)->"id")”]
[mikro@tik] > :put $testx
100
[mikro@tik] > [:parse “global vlancommon;:global testx ($$([:toarray “UP,vlancommon,KIDS”]->1)->"name")”]
[mikro@tik] > :put $testx
VLAN_COMMON
[mikro@tik] > [:parse “global vlancommon;:global testx ($$([:toarray “UP,vlancommon,KIDS”]->1)->"comment")”]
[mikro@tik] > :put $testx
COMMON SERVICES AND DEVICES / OFFICE
[mikro@tik] >
I got
[mikro@tik] > :global vlancommon {id=100;name="VLAN_COMMON";comment="COMMON SERVICES AND DEVICES / OFFICE"}
but not
[mikro@tik] > :global "vlan_common" {id=100;name="VLAN_COMMON";comment="COMMON SERVICES AND DEVICES / OFFICE"}
regardless the spaces.
The problem is Operations with Arrays - Manual:Scripting - MikroTik Wiki
Warning: Key name in array contains any character other than lowercase character, it should be put in quotes
Why you mix the cards?
I talk about spaces on array elements, not underscores…
because I would like to know how to deal with:
:global "vlan_common" {id=100;name="VLAN_COMMON";comment="COMMON SERVICES AND DEVICES / OFFICE"} #variable has to be quoted
ah, on this way, obvious
:global "vlan_common" {id=100;name="VLAN_COMMON";comment="COMMON SERVICES AND DEVICES / OFFICE"}
:put ($"vlan_common")
:put ($"vlan_common"->"name")
:put ([:toarray "UP,vlan_common,KIDS"]->1)
:global testx
[:parse ("global \"vlan_common\";:global testx (\$\"$([:toarray "UP,vlan_common,KIDS"]->1)\"->\"id\")")]
:put $testx
[:parse ("global \"vlan_common\";:global testx (\$\"$([:toarray "UP,vlan_common,KIDS"]->1)\"->\"name\")")]
:put $testx
[:parse ("global \"vlan_common\";:global testx (\$\"$([:toarray "UP,vlan_common,KIDS"]->1)\"->\"comment\")")]
:put $testx
[mikro@tik] > :global “vlan_common” {id=100;name=“VLAN_COMMON”;comment=“COMMON SERVICES AND DEVICES / OFFICE”}
[mikro@tik] > :put ($“vlan_common”)
comment=COMMON SERVICES AND DEVICES / OFFICE;id=100;name=VLAN_COMMON
[mikro@tik] > :put ($“vlan_common”->“name”)
VLAN_COMMON
[mikro@tik] > :put ([:toarray “UP,vlan_common,KIDS”]->1)
vlan_common
[mikro@tik] > :global testx
[mikro@tik] > [:parse (“global "vlan_common";:global testx ($"$([:toarray “UP,vlan_common,KIDS”]->1)" → "id")”)]
[mikro@tik] > :put $testx
100
[mikro@tik] > [:parse (“global "vlan_common";:global testx ($"$([:toarray “UP,vlan_common,KIDS”]->1)" → "name")”)]
[mikro@tik] > :put $testx
VLAN_COMMON
[mikro@tik] > [:parse (“global "vlan_common";:global testx ($"$([:toarray “UP,vlan_common,KIDS”]->1)" → "comment")”)]
[mikro@tik] > :put $testx
COMMON SERVICES AND DEVICES / OFFICE
[mikro@tik] >