Community discussions

 
tomtom80
newbie
Topic Author
Posts: 43
Joined: Sun Jan 06, 2008 12:48 am

how to set an element in an array

Sun Apr 19, 2009 2:56 pm

Hallo,

how to set an element in this array? See example.
[admin@Mikrotik] >:global x aa,bb,cc,dd,ee,ff
[admin@Mikrotik] > :put [:pick $x 2]
cc
With pick command I can read element 2. How to set element 2 in array to an other value? For example how is the command to set element 2 to ccc?

So that the command :put [:pick $x 2] has the result ccc?

Thanks in advance!
 
tomtom80
newbie
Topic Author
Posts: 43
Joined: Sun Jan 06, 2008 12:48 am

Re: how to set an element in an array

Wed Apr 22, 2009 1:57 pm

Does anybody know it?

I'll be satisfied to know if it is possible or not.
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 5934
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: how to set an element in an array

Wed Apr 22, 2009 2:09 pm

As far as I know you can't do it directly.
However you can use something like example above:
:global aaa 1,2,3,4,5
# replace third value with 9
:set aaa ([:pick $aaa 0 2] , "9" , [:pick $aaa 3 5])
:environment print
Output:
"aaa"={"1"; "2"; "9"; "4"; "5"}
 
User avatar
iperezandres
newbie
Posts: 42
Joined: Mon Feb 13, 2017 1:17 pm
Location: Madrid
Contact:

Re: how to set an element in an array

Wed Dec 26, 2018 12:35 pm

Yes, you can, but you need to assign "names" to each value in the array:
:global x {"1"="a";"2"="b";"3"="c";"4"="d"}
:put $x
Output: 1=a;2=b;3=c;4=d
:set ($x->"3") "ccc"
:put $x
Output: 1=a;2=b;3=ccc;4=d

Hope it helps.
 
Frostbyte
Frequent Visitor
Frequent Visitor
Posts: 72
Joined: Mon Dec 25, 2017 1:42 am

Re: how to set an element in an array

Fri Dec 28, 2018 11:38 am

Not necessarily. If you know which index you want to edit you can go like this:

set ($arrayVariable->$arrayIndex) $newValue

Where $arrayIndex is an integer.
 
User avatar
iperezandres
newbie
Posts: 42
Joined: Mon Feb 13, 2017 1:17 pm
Location: Madrid
Contact:

Re: how to set an element in an array

Fri Dec 28, 2018 12:14 pm

You are right, haven't tried that approach!
Not necessarily. If you know which index you want to edit you can go like this:

set ($arrayVariable->$arrayIndex) $newValue

Where $arrayIndex is an integer.

Who is online

Users browsing this forum: No registered users and 14 guests