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: 171
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
 
devquora
just joined
Posts: 1
Joined: Fri Feb 23, 2018 12:10 pm
Location: New Delhi
Contact:

Re: How To Get Keys of this Array ?

Fri Feb 23, 2018 12:14 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)
 
CTSsean
newbie
Posts: 25
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?
 
rocksy
just joined
Posts: 1
Joined: Thu Feb 20, 2020 2:34 pm
Location: Usa
Contact:

Re: How To Get Keys of this Array ?

Thu Feb 20, 2020 2:38 pm

We can get keys of array like that
$array = array( 'one' =>'value', 'two' => 'value2' );
 
TutorialsMate
just joined
Posts: 2
Joined: Sun Nov 22, 2020 1:15 am
Contact:

Re: How To Get Keys of this Array ?

Sun Nov 22, 2020 1:17 am

This old thread solved my query too. Thank you everyone.

Who is online

Users browsing this forum: Jotne and 28 guests