More about arrays

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.

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.

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?

BTW, easiest way to create an empty array:

:local array1 ({});

So no need for :toarray.

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…


Don’t trust translators too much, that means: “you can try it on v7”

yes, but on this case are only for create a comparable example with [:toarray “4,5,6”]

No, I stop being offended to you instanctly after I send you to hell.

OK. I will keep this in mind. :slight_smile:

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)

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

http://forum.mikrotik.com/t/function-of-transferring-the-script-over-ssh/163718/1

Yes, but are not only 2 lines, and concentration is needed.
When I have more time.

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