Community discussions

MikroTik App
 
User avatar
Sertik
Member
Member
Topic Author
Posts: 435
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

More about arrays

Wed Jan 25, 2023 2:42 pm

A lot of people know, but a lot of people don't...

An array should not be copied with a normal "assignment" like :local array1 $array2, because it is passed by reference.
If after such an "assignment" the values ​​of the elements of the first array are changed, then they will also change in the second one! the first array contains a reference to the second.

Below is an example of such behavior:
:local  array1 "empty"
:local  array2 [:toarray "4,5,6"]
:put "-----------"

:put $array1; 
:put $array2
:set array1 $array2
:set ($array1->0) 10

:put "-----------"

:put $array1; 
:put $array2

Console output:

-----------
empty
4;5;6
-----------
10;5;6
10;5;6

And some say that arrays are not passed by reference in Mikrotik ...


But, if after such manipulations the second array is reset to zero, then its elements will remain in the first one
:set array2 [ ]
:put $array1; 
10;5;6

When an array is passed to a function as a parameter, its full copy is passed. Changes that happen to an array in a function will not affect the original array.
# The code creates a global array, passes it to the function
:global arrayTest [:toarray ""]
:set $arrayTest ({a=10;b=20;c=30})
:global arrayChange

:put ("fArray\t\t" . ([:tostr [$arrayChange fArray=$arrayTest]]))
:put ("arrayTest\t" . [:tostr $arrayTest])
# The function changes the elements in the array and returns the changed array
:global arrayChange
:if (!any $arrayChange) do={ :global arrayChange do={

  :set ($fArray->"a") 40
  :set ($fArray->"b") 50
  :set ($fArray->"c") 60
  :return $fArray
  }
}

Let's run the arrayTest function:

[admin@MikroTik] > system/script/run arrayTest 
Console output:
a=40;b=50;c=60
a=10;b=20;c=30

and we see that the original array has not changed !

Maybe this behavior is due to the fact that the array can be a function? That is, before changing the values ​​of an array, it must be declared as a global function?

:global arrayChange
:if (!any $arrayChange) do={ :global arrayChange do={
:global $fArray
:set ($fArray->"a") 40
:set ($fArray->"b") 50
:set ($fArray->"c") 60
:return $fArray
}.

there is no way to check now ... But I think it should be like this.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: More about arrays

Wed Jan 25, 2023 3:30 pm

A lot of people know, but a lot of people don't...
You are one of that people.

Dear doctor, when do test, use double check and also use correct syntax, "empty" do not create one array variable but one string variable.
And also whn you do some test, you must write on what version of RouterOS you do the tests.
{
:local array1 [:toarray ""]
:local array2 [:toarray "4,5,6"]
:put "-----------"
:put $array1
:put $array2

:set array1 $array2
:set ($array1->0) 10
:set ($array2->1) 20

:put "-----------"
:put $array1
:put $array2
}


And some say that arrays are not passed by reference in Mikrotik ...
Yes and SOME already wrote which is just a bug in the compiler already fixed on RouterOS v7.(I don't remember the number)
Just read and understand all.

But just check on current 7.7 and do not happen.
Last edited by rextended on Wed Jan 25, 2023 3:45 pm, edited 1 time in total.
 
User avatar
Sertik
Member
Member
Topic Author
Posts: 435
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: More about arrays

Wed Jan 25, 2023 3:45 pm

Hello Rex! I confess that I didn’t put the code in the local area on purpose, wanting to check whether others will find my mistake ... Well, it’s clear that you instantly found it!
Yes, and I am very glad that you stopped being offended by me as you answered ...
I didn't know about fixing the bug in version 7, thank you for the explanation. I don't have version 7 yet (only the latest version 6 - 6.46.9 seems). And why do you write that it will not work to check on version 7.7? Maybe I misunderstood the translation or are these Mikrotik sanctions?
 
User avatar
eworm
Forum Guru
Forum Guru
Posts: 1070
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: More about arrays

Wed Jan 25, 2023 3:46 pm

BTW, easiest way to create an empty array:
:local array1 ({});
So no need for :toarray.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: More about arrays

Wed Jan 25, 2023 3:49 pm

Yes, and I am very glad that you stopped being offended by me as you answered ...
No, I stopped being offended by you right after I sent you to hell. (for the other users: see previous episode)
I have no problem answering questions, I don't want to answer counter-questions...

And why do you write that it will not work to check on version 7.7? Maybe I misunderstood the translation or are these Mikrotik sanctions?
Don't trust translators too much, that means: "you can try it on v7"
Last edited by rextended on Wed Jan 25, 2023 3:59 pm, edited 3 times in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: More about arrays

Wed Jan 25, 2023 3:52 pm

So no need for :toarray.
yes, but on this case are only for create a comparable example with [:toarray "4,5,6"]
 
User avatar
Sertik
Member
Member
Topic Author
Posts: 435
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: More about arrays

Wed Jan 25, 2023 3:58 pm

No, I stop being offended to you instanctly after I send you to hell.
OK. I will keep this in mind. :)
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: More about arrays

Wed Jan 25, 2023 4:00 pm

Sorry, translated with feet, I corrected the post:
No, I stopped being offended by you right after I sent you to hell. (for the other users: see previous episode)
Last edited by rextended on Wed Jan 25, 2023 4:01 pm, edited 1 time in total.
 
User avatar
Sertik
Member
Member
Topic Author
Posts: 435
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: More about arrays

Wed Jan 25, 2023 4:01 pm

Rex, if you don't mind me already, could you take a look at my SSH script transfer function?

viewtopic.php?t=192775
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: More about arrays

Wed Jan 25, 2023 4:07 pm

Yes, but are not only 2 lines, and concentration is needed.
When I have more time.
 
User avatar
Sertik
Member
Member
Topic Author
Posts: 435
Joined: Fri Sep 25, 2020 3:30 pm
Location: Russia, Moscow

Re: More about arrays

Wed Jan 25, 2023 4:13 pm

OK, I'll be glad to wait for your attention. Let's move the discussion to a post with this feature...

Who is online

Users browsing this forum: No registered users and 21 guests