Community discussions

MikroTik App
 
Tal
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 57
Joined: Wed Jun 17, 2015 2:17 am

[SOLVED] Using Dynamic Variable Names

Sat Aug 15, 2015 5:45 am

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?
Last edited by Tal on Fri Aug 28, 2015 6:56 am, edited 1 time in total.
 
User avatar
favincen
just joined
Posts: 21
Joined: Mon Jun 08, 2015 1:56 pm
Location: Grenoble, France

Re: Using Dynamic Variable Names

Thu Aug 27, 2015 3:59 pm

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.
If you consider this post useful, you can tell by rating it positively... Thanks ! 8)
 
User avatar
favincen
just joined
Posts: 21
Joined: Mon Jun 08, 2015 1:56 pm
Location: Grenoble, France

Re: Using Dynamic Variable Names

Thu Aug 27, 2015 4:53 pm

Using ":execute" instead of ":parse", it works 8) :
:foreach varname in={"VAR1" ; "VAR2" ; "VAR3"  } do={:execute ":global $varname \$VALUE" }
If you consider this post useful, you can tell by rating it positively... Thanks ! 8)
 
Tal
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 57
Joined: Wed Jun 17, 2015 2:17 am

Re: Using Dynamic Variable Names

Fri Aug 28, 2015 6:55 am

Using ":execute" instead of ":parse", it works 8) :
:foreach varname in={"VAR1" ; "VAR2" ; "VAR3"  } do={:execute ":global $varname \$VALUE" }
I used $VALUE instead of \$VALUE, but it worked perfectly.

Thanks guys.
 
User avatar
favincen
just joined
Posts: 21
Joined: Mon Jun 08, 2015 1:56 pm
Location: Grenoble, France

Re: Using Dynamic Variable Names

Fri Aug 28, 2015 11:26 am

I used $VALUE instead of \$VALUE, but it worked perfectly.
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... :wink:

BTW, a warning: I may have missed something but AFAIK, if the ":execute" f command fails, you get no error message of any kind. :oops:

Fabrice
If you consider this post useful, you can tell by rating it positively... Thanks ! 8)
 
Tal
Frequent Visitor
Frequent Visitor
Topic Author
Posts: 57
Joined: Wed Jun 17, 2015 2:17 am

Re: Using Dynamic Variable Names

Fri Aug 28, 2015 7:01 pm

I used $VALUE instead of \$VALUE, but it worked perfectly.
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... :wink:

BTW, a warning: I may have missed something but AFAIK, if the ":execute" f command fails, you get no error message of any kind. :oops:

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
just joined
Posts: 4
Joined: Fri Sep 12, 2014 5:39 pm

Re: Using Dynamic Variable Names

Sat Aug 04, 2018 1:13 pm

This code doesn't work. But it may give a hint toward a solution :

You just need square brackets:
foreach varname in={"VAR1"; "VAR2"; "VAR3"} do={[parse "global $varname true"]}
 
randomwalk
just joined
Posts: 9
Joined: Sun Apr 21, 2013 3:40 am
Location: Canada

Re: Using Dynamic Variable Names

Sat Apr 13, 2019 11:28 pm

   
Last edited by randomwalk on Wed Dec 16, 2020 9:45 am, edited 1 time in total.
 
randomwalk
just joined
Posts: 9
Joined: Sun Apr 21, 2013 3:40 am
Location: Canada

Re: Using Dynamic Variable Names

Wed Dec 16, 2020 9:44 am

Works with global, but not possible with local. Is the any solution for local variables?
Store the dynamically created local variables in an array.

{
    :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") ] )
}

 
output:
[admin@rb] >
myvar1=plaintext type=str
myvar2=abc type=array
myvar3= type=nothing
myvar4=true type=bool
myvar5=127.0.0.1 type=ip
myvar6=23:59:59 type=time
myvar7=192.168.88.0/24 type=ip-prefix
myvar8=::ffff:192.0.2.128 type=ip6
myvar9=2001:db8:0:b::/64 type=ip6-prefix

Who is online

Users browsing this forum: cashwu, willyfontana and 24 guests