Community discussions

MikroTik App
 
User avatar
astounding
Member Candidate
Member Candidate
Topic Author
Posts: 121
Joined: Tue Dec 16, 2008 12:17 am

:toarray bug???

Sat Nov 22, 2014 12:56 am

First, let's see what happens if one casts an existing normal array to an array using :toarray:
{
  :local a (1,2,3);
  :local b [:toarray $a];
  :put "a is $[:typeof $a] len $[:len $a]:";
  :put $a;
  :put "b is $[:typeof $b] len $[:len $b]:";
  :put $b;
  :put "equiv...";
  :if ($a=$b) do={:put "yes"} else={:put "no"}
}
OUTPUT:
a is array len 3:
1;2;3
b is array len 3:
1;2;3
equiv...
yes
NOW just change the initialization of the array to an associative array (I'm running RouterOS 6.22, FYI):
  :local a {"a"="A";"b"="B";"c"="C"};
BUGGY? OUTPUT:
a is array len 3:
a=A;b=B;c=C
b is array len 0:

equiv...
no
It sure seems to me that casting an associative array using :toarray should leave it unchanged.

(Please don't ask the naive question "Why would you ever need to cast an array to an array if it's already an array?" Asking that likely means you've not tried writing robust scripts that expect an array as input but can gracefully handle non-array data too in certain cases. If casting had worked as expected, then it would permit handling input data in several forms without having to add code to test input data types.)

Is this a bug?

Wondering...

Aaron out.
 
User avatar
janisk
MikroTik Support
MikroTik Support
Posts: 6263
Joined: Tue Feb 14, 2006 9:46 am
Location: Riga, Latvia

Re: :toarray bug???

Tue Nov 25, 2014 12:23 pm

That is a bug.

Wait for next release of RouterOS for almost fixed version.

toarray will pass this array through
making this:
:local b [:toarray $a];
into this if $a is an array of any kind:
:local b  $a;
but if you construct string that would create arrays with key=value pairs, it will not parse it correctly. So it would be more useful for us to see your script to see what problem exactly you are having (or possible inputs you are fighting) to correct the problem as for now - the fix is kind of hack (hence the "almost")
 
User avatar
astounding
Member Candidate
Member Candidate
Topic Author
Posts: 121
Joined: Tue Dec 16, 2008 12:17 am

Re: :toarray bug???

Fri Dec 05, 2014 10:27 pm

True to your word, 6.23 fixes the issue:
{
  :local a {"a"="A";"b"="B";"c"="C"};
  :local b [:toarray $a];
  :put "a is $[:typeof $a] len $[:len $a]:";
  :put $a;
  :put "b is $[:typeof $b] len $[:len $b]:";
  :put $b;
  :put "equiv...";
  :if ($a=$b) do={:put "yes"} else={:put "no"}
}
...now gives me the expected:
a is array len 3:
a=A;b=B;c=C
b is array len 3:
a=A;b=B;c=C
equiv...
yes
Thanks!

Who is online

Users browsing this forum: No registered users and 67 guests