Arrays and complex variable creation

I’m tyring to write a script for managing my blacklists. There is one part I am stuck with and would be grateful for some help.

The code below does not work and I just cannot find any appropiate examples of how to solve it. I need to know how to create the variable. Notice that I need to create the variable name from data in the array. Any ideas?

Thanks, Charles

# I want to create a variable named "MyVariable100" with a value of "myblacklist"
:local myArray {"Confidence"="100";"mylist"="myblacklist";"mytimeout"="static"}
[:parse :global MyVariable$myArray->"Confidence" $myArray->"myList" ]
/log info "the value is $MyVariable100"

:scissors: Rextended Fragments of Snippets


dynamic variables
http://forum.mikrotik.com/t/concatenate-values-to-create-variable-name/151793/2

if you have any questions, ask them here

This is a mess to understand but…

Working example

{
:local myArray {"Confidence"="100";"mylist"="myblacklist";"mytimeout"="static"}

:global variablename "MyVariable$($myArray->"Confidence")"
[:parse ":global $variablename \"$($myArray->"mylist")\""]
:global variablevalue
[:parse "global $variablename;:global variablevalue \$$variablename"]

:put "the variable $variablename contain $variablevalue"
}

Thanks but my version of it doesn’t work. The variable is set but it is not output to the log in the last line. Does it need to be declared before use?

:local myArray {"Confidence"="100";"mylist"="myblacklist";"mytimeout"="static"}

:global variablename "MyVariable$($myArray->"Confidence")"
[:parse ":global $variablename \"$($myArray->"mylist")\""]

/log info "the variable value is $MyVariable100 "

You have already a working example…

Your working example cannot log the variable itself. I’ve added the variable to the end of your example (I use Scripts to test, not the console). You try it.

:local myArray {"Confidence"="100";"mylist"="myblacklist";"mytimeout"="static"}

:global variablename "MyVariable$($myArray->"Confidence")"
[:parse ":global $variablename \"$($myArray->"mylist")\""]
:global variablevalue
[:parse "global $variablename;:global variablevalue \$$variablename"]

/log info "the variable $variablename contain $variablevalue. $MyVariable100"

Just replace “:put” with “:log info”, and if I put the code between { } it there is a reason, if you test it on terminal…

What you have added ($ MyVariable 100) cannot work
because it assumes that RouterOS already knows in advance how the variables will be called and defined before the script is compiled.

Ok.

That is what I was trying to achieve, so it’s a shame I can’t do that. But thanks for your help. I was also trying to figure out how to access array elements, so now I know. The mikrotik site is pretty bad for things like that.

Have you thought of doing a Confluence site or wiki to capture all your mikrotik knowledge?

I don’t know all programming languages,
but of the ones I know, on none you can call a variable after compilation, which has not already been created and exists previously…



Thanks, I’m flattered, but I wouldn’t have time, already in my signature I make available things here and already made, to use as an example…