Community discussions

MikroTik App
 
jeroenp
Member Candidate
Member Candidate
Topic Author
Posts: 159
Joined: Mon Mar 17, 2014 11:30 am
Location: Amsterdam
Contact:

Bug? How can I create an empty associative array, then fill it to map strings to strings?

Thu May 05, 2016 6:05 pm

I'm trying to create an empty associative array that maps strings to strings, then add a few associations. It fails on all methods I tried.

How can I create an empty associative array, then fill it to map strings to strings?
Which of the issues below are bugs and how can I report them?

The associations I want to have to do are related to escaping strings so non-printable characters are HEX-encoded:
"\00"->"\\00"
"\01"->"\\01"
..
"\1F"->"\\1F"
"\7F"->"\\7F"
..
"\FF"->"\\FF"
I don't want printable characters in the association.

Below is what fails and how. I've included a `/system script environment remove 0` (on a system without any global variables set) in-between all examples.



Fails because the initial value isn't parsed correctly (and has a trailing double-quote so `\00` isn't associated):
:global convert {"\00"="\\00"}
:set ($convert->"\7F") "\\7F"
:set ($convert->"\81") "\\81"
:put [/system script environment print]
 # NAME               VALUE
 0 convert            \81=\81;\00"=\00;\7F=\7F

:put [:typeof $convert]
array
:put "$convert"
?=\81;"=\00;=\7F
:put ($convert->"\00")

:put ($convert->"\81")
\81
:put ($convert->"f")
Fails because the initial value isn't parsed correctly (and has a trailing double-quote so `\81` isn't associated):
:global convert {"\81"="\\81"}
:set ($convert->"\7F") "\\7F"
:set ($convert->"\00") "\\00"
:put [/system script environment print]
 # NAME               VALUE
 0 convert            \81"=\81;\00=\00;\7F=\7F

:put [:typeof $convert]
array
:put "$convert"
?"=\81;=\00;=\7F
:put ($convert->"\00")
\00
:put ($convert->"\81")

:put ($convert->"f")
Fails because `$convert` becomes a `nothing` to which you cannot add association values:
:global convert value=
:set ($convert->"\00") "\\00"
:set ($convert->"\7F") "\\7F"
:set ($convert->"\81") "\\81"
:put [/system script environment print]
 # NAME               VALUE
 0 convert

:put [:typeof $convert]
nothing
:put "$convert"

:put ($convert->"\00")

:put ($convert->"\81")

:put ($convert->"f")
Fails because the initial value isn't parsed correctly (and has a trailing double-quote so `\01` isn't associated):
:global convert {"\01"="\\01"}
:set ($convert->"\7F") "\\7F"
:set ($convert->"\81") "\\81"
:put [/system script environment print]
 # NAME               VALUE
 0 convert            \81=\81;\01"=\01;\7F=\7F

:put [:typeof $convert]
array
:put "$convert"
?=\81;"=\01;=\7F
:put ($convert->"\00")

:put ($convert->"\81")
\81
:put ($convert->"f")
Fails because `$convert` cannot be declared with an empty association:
:global convert {}
syntax error (line 1 column 19)
:set ($convert->"\00") "\\00"
:set ($convert->"\7F") "\\7F"
:set ($convert->"\81") "\\81"
:put [/system script environment print]
 # NAME               VALUE

:put "$convert"

:put ($convert->"\00")

:put ($convert->"\81")

:put ($convert->"f")
Fails because only the first association is stored as such; the rest of the array values are boolean results of string comparisons:
:global convert {"o"="o","f"="f","b"="a"}
:put [/system script environment print]
 # NAME               VALUE
 0 convert            o=o;true;false

:put [:typeof $convert]
array
:put "$convert"
o=o;true;false
:put ($convert->"\00")

:put ($convert->"\81")

:put ($convert->"f")
Sort of works because I initialised with an association that will never be used, but I cannot remove that association so technically this fails too:
:global convert {"##"=""}
:set ($convert->"\00") "\\00"
:set ($convert->"\7F") "\\7F"
:set ($convert->"\81") "\\81"
:put [/system script environment print]
 # NAME               VALUE
 0 convert            \81=\81;\00=\00;##=;\7F=\7F

:put [:typeof $convert]
array
:put "$convert"
?=\81;=\00;##=;=\7F
:put ($convert->"\00")
\00
:put ($convert->"\81")
\81
:put ($convert->"f")

--jeroen
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Bug? How can I create an empty associative array, then fill it to map strings to strings?

Fri May 06, 2016 1:51 am

You can create an empty array by surrounding the "{}" with "()", i.e.
:global convert ({});
The syntax error seems is a left over because of what MikroTik might've intended to allow at one point (creating functions, without requiring the "do" argument) vs. what actually ended up happening ("do" is required always, precisely because arrays can end up in "value", and there's no way to tell which is which).

You can report bugs to support at support@mikrotik.com, although I can tell you from personal experience you shouldn't expect scripting bugs to be resolved quickly (as in next release or the next one after that), but they will be noted, so that shouldn't discourage you from reporting them.
 
jeroenp
Member Candidate
Member Candidate
Topic Author
Posts: 159
Joined: Mon Mar 17, 2014 11:30 am
Location: Amsterdam
Contact:

Re: Bug? How can I create an empty associative array, then fill it to map strings to strings?

Fri May 06, 2016 8:35 am

You can create an empty array by surrounding the "{}" with "()", i.e.
:global convert ({});
...
You can report bugs to support at support@mikrotik.com ...
Thanks.
Which of the above do you regard as bugs?

--jeroen
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2400
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Bug? How can I create an empty associative array, then fill it to map strings to strings?

Fri May 06, 2016 1:46 pm

I'd say everything, except the
Fails because `$convert` becomes a `nothing` to which you cannot add association values:
part.

The default value for variables is indeed "nothing", and the fact you can't append to "nothing" makes sense in every way.

Also, while the
Fails because `$convert` cannot be declared with an empty association:
part is a bug, the fact you can easily work around it would make me think of it as a very low priority if I was in MikroTik's shoes.
 
jeroenp
Member Candidate
Member Candidate
Topic Author
Posts: 159
Joined: Mon Mar 17, 2014 11:30 am
Location: Amsterdam
Contact:

Re: Bug? How can I create an empty associative array, then fill it to map strings to strings?

Fri May 06, 2016 4:50 pm

I'd say everything, except the
Fails because `$convert` becomes a `nothing` to which you cannot add association values:
part.
...
Thanks. I will report them in due time.

--jeroen

Who is online

Users browsing this forum: No registered users and 13 guests