Community discussions

MikroTik App
 
msatter
Forum Guru
Forum Guru
Topic Author
Posts: 2013
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Cumbersome fetching the key in an array and duplicating arrays

Wed Nov 04, 2020 2:57 am

I want to return the key in a array and don't want to use the "foreach k,v in $ar" method.
:local ar {"Abcdef"="b";c="d"}; :local a [:tostr [:pick $ar 0 1]]; :put [:pick $a 0 ([:find $a "="])]

[:tostr [:pick $ar 0 1]]; is putting out the key and the value : Abcdef=b. If you do: [:tostr [:pick $ar 1 2]]; you get the next key,value: c=d
When you leave the first number on zero and increase the second one you get more elements in one go.

Is there a more direct way to get the only the key (Abcdef)?
Last edited by msatter on Sat Nov 07, 2020 3:38 am, edited 2 times in total.
Loving my freedom and so, no Twitter, no Facebook/Instagram/WhatsApp, no Apple and no Google/Alphabet.

Running:
RouterOS 6.48 / Winbox 3.27 64bits
 
User avatar
Shumkov
just joined
Posts: 14
Joined: Tue Oct 01, 2019 9:08 pm
Location: Russian Federation

Re: Cumbersome fetching the key in an array

Thu Nov 05, 2020 8:49 am

Two-dimensional array?
[admin@MikroTik] >> local ar {{"Abcdef";"a"};{"c";"d"}};put ($ar->0->0);put ($ar->1->0)
Abcdef
c
RB951G-2HnD / RouterOS 6.46.8 (Long-term)
 
msatter
Forum Guru
Forum Guru
Topic Author
Posts: 2013
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: Cumbersome fetching the key in an array

Fri Nov 06, 2020 8:46 pm

I had a look at 2D array. They are not easy and I stepped in a few beartraps and maybe I even need some new functions to handle them easier.

Some things are easier to archive but it is a lot to learn and test.

Update: I have converted it from array with a key to a 2D array. Work great and it was a few hours checking and finding out how to use it. Thanks for pointing me in the right direction.

I had one problem of appending to the 2D array but thanks to this posting that is not tackled also:

viewtopic.php?p=819886#p728850

Example transfering specific item from one 2D to an other 2D:
:foreach label,v in=$arrayLabel2D do={:set $arrayDisplay2D ($arrayDisplay2D , {($arrayLabel2D->$label->1),""}) };
This appends a item and adds also a empty item so that later it can be used. I use :foreach to have a stop at the end of the source array and $label is then just a counter. This because there are not keys in the array.

:foreach has become my favorite script command by now, it is so flexible. The $v contains the two items in a array part separated by ";" it is just bonus that can be used in other ways like moving two item in one go, to the destination array.
:global arrayDisplay2D [:toarray ""]; :foreach label,v in=$arrayLabel2D do={:set $arrayDisplay2D ($arrayDisplay2D , {$v}) }
The Wiki could contain so much more examples and it saves users a lot of time trying and searching.

Nice to find these gems.
Loving my freedom and so, no Twitter, no Facebook/Instagram/WhatsApp, no Apple and no Google/Alphabet.

Running:
RouterOS 6.48 / Winbox 3.27 64bits
 
msatter
Forum Guru
Forum Guru
Topic Author
Posts: 2013
Joined: Tue Feb 18, 2014 12:56 am
Location: Netherlands / Nīderlande

Re: Cumbersome fetching the key in an array and duplicating arrays

Sat Nov 07, 2020 11:17 am

BTW, I managed to wipe environment when a Array was by mistake looped and the memory start to fill up and after about 20MB it would clear and start again to fill up.

In Winbox the environment screen was wiped and in red was displayed something "NOTHING FOUND". Then filtering would engage on it's own and both field displayed the word "NOTHING".

For me the only way to stop this was rebooting the router despite all was working. But I wanted to have the environment screen back to check my script.
Loving my freedom and so, no Twitter, no Facebook/Instagram/WhatsApp, no Apple and no Google/Alphabet.

Running:
RouterOS 6.48 / Winbox 3.27 64bits

Who is online

Users browsing this forum: No registered users and 32 guests