Community discussions

MikroTik App
 
howdey57
Member Candidate
Member Candidate
Topic Author
Posts: 122
Joined: Wed Dec 31, 2014 2:36 pm

Arrays and complex variable creation

Thu Nov 03, 2022 8:46 pm

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"
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Arrays and complex variable creation

Thu Nov 03, 2022 8:54 pm

✂ Rextended Fragments of Snippets


if you have any questions, ask them here
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Arrays and complex variable creation

Thu Nov 03, 2022 9:16 pm

# 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"
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"
}
 
howdey57
Member Candidate
Member Candidate
Topic Author
Posts: 122
Joined: Wed Dec 31, 2014 2:36 pm

Re: Arrays and complex variable creation

Fri Nov 04, 2022 9:58 am

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 "
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Arrays and complex variable creation

Fri Nov 04, 2022 10:17 am

You have already a working example...
 
howdey57
Member Candidate
Member Candidate
Topic Author
Posts: 122
Joined: Wed Dec 31, 2014 2:36 pm

Re: Arrays and complex variable creation

Fri Nov 04, 2022 11:02 am

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"
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Arrays and complex variable creation

Fri Nov 04, 2022 4:28 pm

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.
 
howdey57
Member Candidate
Member Candidate
Topic Author
Posts: 122
Joined: Wed Dec 31, 2014 2:36 pm

Re: Arrays and complex variable creation

Fri Nov 04, 2022 7:14 pm

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?
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11967
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Arrays and complex variable creation

Fri Nov 04, 2022 9:23 pm

[…] it's a shame I can't do that […]
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...


Have you thought of doing a Confluence site or wiki to capture all your mikrotik knowledge?
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...

Who is online

Users browsing this forum: No registered users and 18 guests