Community discussions

MikroTik App
 
rftnon
newbie
Topic Author
Posts: 29
Joined: Fri Feb 28, 2014 6:34 pm

How To Get Keys of this Array ?

Sun Feb 12, 2017 11:25 pm

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
Member Candidate
Member Candidate
Posts: 172
Joined: Mon Feb 04, 2013 7:00 pm
Contact:

Re: How To Get Keys of this Array ?

Mon Feb 13, 2017 5: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
newbie
Topic Author
Posts: 29
Joined: Fri Feb 28, 2014 6:34 pm

Re: How To Get Keys of this Array ?

Fri Feb 17, 2017 5: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
 
CTSsean
Frequent Visitor
Frequent Visitor
Posts: 61
Joined: Fri Sep 15, 2017 12:56 pm

Re: How To Get Keys of this Array ?

Fri May 04, 2018 6:37 pm

The array_keys() function is used to get all the keys or a subset of the keys of an array.
Here is syntax

array_keys(input_array, search_key_value, strict)
from my testing... this function doesn't exist within RouterOS. Can you give me a syntax example?
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3255
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: How To Get Keys of this Array ?

Wed Jul 26, 2023 10:29 pm

Ironically, the :convert command added in 7.11 that will do the particular conversion in original post.
[...]
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"

No array lookup, or any array, needed, at least for the rot13 decrypt...
:put [:convert "c" to=rot13]
# p

Who is online

Users browsing this forum: No registered users and 19 guests