Community discussions

 
rftnon
just joined
Topic Author
Posts: 16
Joined: Fri Feb 28, 2014 7:34 pm
Reputation: 0

How To Get Keys of this Array ?

Mon Feb 13, 2017 12:25 am

How to Get Key entry in associative array?

what about the other way around and remove an entry form an associative array?

how to Get Key the element with value `P` in the code below:

:global rot13 ({});
:set ($rot13->"a") "n"
:set ($rot13->"b") "o"
:set ($rot13->"c") "p"

thanks
 
User avatar
BlackVS
Frequent Visitor
Frequent Visitor
Posts: 97
Joined: Mon Feb 04, 2013 8:00 pm
Reputation: 6
Contact:

Re: How To Get Keys of this Array ?

Mon Feb 13, 2017 6:01 pm

So simple things are still so crazy in RouterOS.
1. First approach is like here - http://www.paperstreetonline.com/catego ... /mikrotik/
2. In the case if known index of element to be removed I prefer use such more simple way (removing element with index $index from array):
:put ([:pick $rot13 0 $index],[:pick $rot13 ($index+1) [:len $rot13]])

I.e. to remove element you construct new array and assign it to the same name:
:set rot13 ([:pick $rot13 0 $index],[:pick $rot13 ($index+1) [:len $rot13]])

Delete by key is much simpler, just write command below to remove element with key "b":
:set ($rot13->"b")


3. It is crazy but :find not works with such arrays. You have to use foreach loop and search index or key manually by checking each element.
Example of iterating through keys/values:
:foreach k,v in=$rot13 do={:put ($k,$v)};

or by index/values:
:for i from=0 to=([:len $rot13]-1) do={:put ($i,[:pick $rot13 $i])};
 
rftnon
just joined
Topic Author
Posts: 16
Joined: Fri Feb 28, 2014 7:34 pm
Reputation: 0

Re: How To Get Keys of this Array ?

Fri Feb 17, 2017 6:37 pm

Thank you very much dear

Very sorry for being late to respond to health conditions

your answer %100
The problem has been resolved

Thank you very much

Who is online

Users browsing this forum: No registered users and 3 guests