Community discussions

MikroTik App
 
sin3vil
newbie
Topic Author
Posts: 34
Joined: Sat May 26, 2018 10:05 pm

Call variable by dynamically constructing it's name

Sat May 26, 2018 10:47 pm

Hello all, I'm not sure if my title is descriptive enough.
I want to do this :
local var1 "1st variable"
local var2 "2nd variable"
....
local varn "nth variable"
local numofvars n

for i from=1 to=$numofvars do={
:put $("var".$i)
}

Expected output:
1st variable
2nd variable
...
nth variable
But I'm unable to sucessfully construct varX and call it as a variable.
Is this even possible?
 
User avatar
dasiu
Trainer
Trainer
Posts: 231
Joined: Fri Jan 30, 2009 11:41 am
Location: Reading, UK
Contact:

Re: Call variable by dynamically constructing it's name

Mon Jun 04, 2018 2:20 pm

Use ARRAYS for this :). For example:
### Initiating the array
:global xxx {1="1st value";2="2nd value";3="3rd value";n="nth value"}

###Getting all values (with the keys) from the array
:foreach key,value in=$xxx do={
  :put "Table entry number $key has the value: $value"
}

###Changing the existing value in the array
:put "Changing 3rd variable"
:set ($xxx->"3") "Different 3rd value :)"

###Reading the specific variable
:local var3 ($xxx->"3")
:put "Variable 3 is: $var3"

###The problem is, that if the value is not already there, to add new value we need to add the existing array to "new" value with concatenation, like:
:put "Adding 5th variable"
:set $xxx ($xxx,{5="5th value"})

###And even more problematic, to add by script, we need to use "execute", like:
:for n from=15 to=19 do={
  :put "Setting value $n"
  :execute ":set \$xxx (\$xxx,{$n=\"$n-th value\"})"
}

###But the result is as it should be :D
:local varsize [:len $xxx]
:put "The array now has $varsize elements:"
:foreach key,value in=$xxx do={
  :put "Table entry number $key has the value: $value"
}

###And... unfortunately to use :execute we need this array to be global. After running the script we can remove it:
/system script environment remove [/sys script environment find name="xxx"]
 
User avatar
dasiu
Trainer
Trainer
Posts: 231
Joined: Fri Jan 30, 2009 11:41 am
Location: Reading, UK
Contact:

Re: Call variable by dynamically constructing it's name

Mon Jun 04, 2018 2:22 pm

BTW - if anyone found a better way to ADD new key to an array, please share! :)
 
User avatar
dasiu
Trainer
Trainer
Posts: 231
Joined: Fri Jan 30, 2009 11:41 am
Location: Reading, UK
Contact:

Re: Call variable by dynamically constructing it's name

Mon Jun 04, 2018 5:05 pm

OK,

I found the way :). The part I was missing was - when doing :set ($array->$key) $value - the $key needs to be string, cannot be integer. So now the proper example is as follows:
### Initiating the array
:local xxx {1="1st value";2="2nd value";3="3rd value";n="nth value"}

###Getting all values (with the keys) from the array
:foreach key,value in=$xxx do={
  :put "Table entry number $key has the value: $value"
}

###Changing the existing value in the array
:put "Changing 3rd variable"
:set ($xxx->"3") "Different 3rd value :)"

###Reading the specific variable
:local var3 ($xxx->"3")
:put "Variable 3 is: $var3"

###Adding a new value
:put "Adding 5th variable"
:set ($xxx->"5") "5th value"

###When adding the value with a script, we need to make sure the array "key" is string, not integer, so - for example:
:for n from=15 to=19 do={
  :local number [:tostr $n]
  :put "Setting value $n"
  :set ($xxx->$number) "$n-th value"
}

###The result is as it should be :D
:local varsize [:len $xxx]
:put "The array now has $varsize elements:"
:foreach key,value in=$xxx do={
  :put "Table entry number $key has the value: $value"
}

Who is online

Users browsing this forum: No registered users and 19 guests