Community discussions

MikroTik App
 
pxcorp
just joined
Topic Author
Posts: 11
Joined: Thu Jun 15, 2017 7:48 pm

Problem with array constant

Mon Jun 20, 2022 12:18 am

Hello I have problem with reusing associative array initializer in loop - first pass works OK, but next passes initialization not working. It is also reproducible in terminal console:
:foreach i in={"a";"b";"c"} do={:local x [:toarray ""]; :set x {"a"=1;"b"=1;"c"=1}; set ($x->$i) 2; :put [:tostr $x];};

a=2;b=1;c=1
a=2;b=2;c=1
a=2;b=2;c=2
(ros 6.49.6)

update: It is not working either with nonassociative array a also when done indirectly via another variable. And what is evene worse - it is changing original variable uset for setting value. So it looks like that inside :set command array is not copied but referenced.
:local y {1;1;1;1};:foreach i in={1;2;3} do={:local x [:toarray ""]; :set x $y; set ($x->$i) 2; :put [:tostr $x];:put [:tostr $y];};

1;2;1;1
1;2;1;1
1;2;2;1
1;2;2;1
1;2;2;2
1;2;2;2

Can anybody explain this to me ?
Last edited by pxcorp on Mon Jun 20, 2022 12:33 am, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Problem with array constant

Mon Jun 20, 2022 12:29 am

missing : here: "; set"

But I do not see anything strange, what you expect?


EDIT:
still missing : here: "; set" on both examples
 
pxcorp
just joined
Topic Author
Posts: 11
Joined: Thu Jun 15, 2017 7:48 pm

Re: Problem with array constant

Mon Jun 20, 2022 12:38 am

missing : here: "; set"
Yes that is typo in command, but it works exactly same way with and without : - in both cases parser not complain about syntax error and behavior is exactly same.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Problem with array constant

Mon Jun 20, 2022 12:43 am

Still not change that is work exactly as expected, what is your problem?
 
pxcorp
just joined
Topic Author
Posts: 11
Joined: Thu Jun 15, 2017 7:48 pm

Re: Problem with array constant

Mon Jun 20, 2022 12:54 am

Still not change that is work exactly as expected, what is your problem?
totally wrong result of script
right result should be
a=2;b=1;c=1
a=1;b=2;c=1
a=1;b=1;c=2

Just found that in ros v7.3.1 it works correctly (and if set is with : doesn't matter to):
:foreach i in={"a";"b";"c"} do={:local x [:toarray ""]; :set x {"a"=1;"b"=1;"c"=1}; set ($x->$i) 2; :put [:tostr $x];};

a=2;b=1;c=1
a=1;b=2;c=1
a=1;b=1;c=2


:foreach i in={"a";"b";"c"} do={:local x [:toarray ""]; :set x {"a"=1;"b"=1;"c"=1}; :set ($x->$i) 2; :put [:tostr $x];};

a=2;b=1;c=1
a=1;b=2;c=1
a=1;b=1;c=2
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11968
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Problem with array constant

Mon Jun 20, 2022 12:58 am

You mentioned v6, on v7 it works differently, it's this way by design.

And about ":", your perseverance to write it on wrong way is really silly, until you work on "root" works, but after you change the "working path", assume a different function...
and : say to the "compiler" that you want use the "root" command set, and not the "set" on local context...
{
/ip
:local test ""
:set test "y"
set test "x"
}
 
pxcorp
just joined
Topic Author
Posts: 11
Joined: Thu Jun 15, 2017 7:48 pm

Re: Problem with array constant

Mon Jun 20, 2022 1:25 am

You mentioned v6, on v7 it works differently, it's this way by design.
I don't know how is :set command implemented inside ros v6 and v7, in fact probably only few people know that. But it does'n matter - it should assign value of second parameter to first one.
In manual there is exactly written: ":set <var> [<value>] assign value to declared variable." - this seems perfectly clear and logical.
But my question is why (in ros v6) :set x {"a"=1;"b"=1;"c"=1} assign to x {"a"=1;"b"=1;"c"=1} in first pass, {"a"=2;"b"=1;"c"=1} in second and {"a"=2;"b"=2;"c"=1} in third - that is not much expected behavior.
Maybe I missed something important in manual, but if not then it looks like that :set implementation for array in ros v6 is somehow broken.

And to ":", it should be there, I misstyped it when testing behaviour from console. Parser doesn't protest, you noticed that is missing. Just to be sure that problem is not caused by that I tested both variants first in v6 and then in v7 to have exactly same testing scenario. Teoretically it should not work without :, but somehow it doesnt matter to parser. But that is not problem which I talking about - problem is that incorrect value end up in variable.

PS:
Ive found this when writing quite complex script (after hour of debugging why first item is processed corectly and subseqent items not) and posted code is cutted down to show exact where I see problem.

Who is online

Users browsing this forum: aoravent, Ellaham and 25 guests