Community discussions

MikroTik App
 
User avatar
NathanA
Forum Veteran
Forum Veteran
Topic Author
Posts: 829
Joined: Tue Aug 03, 2004 9:01 am

Searching multidimensional arrays

Mon May 02, 2022 12:06 pm

Because dynamic interfaces can come and go, I have a need to make a snapshot of what interfaces exist at a given point in time, and then search through the snapshot rather than do a real-time search.

For example, instead of...
/interface find name~"ether"
...I want to first capture & store the existing interfaces to an array of an array...
:local $interfaces [/interface print detail as-value]
...and then search through that multidimensional array the same way I would through the active interfaces array (e.g. by being able to reference particular array keys), like so:
:find $interfaces name~"ether"
The hope is that I would get back a list of IDs (perhaps representing positions within the array) of matches. But of course :find doesn't seemingly work like that.

I can of course search through the entire contents of the array for a regex match...
:put ($interfaces~"ether")                
true
...but that's not exactly helpful, now is it?

Really hoping this is possible to do without iterating over each array member, which is very much Not Quick. Thanks for any leads.
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7042
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: Searching multidimensional arrays

Mon May 02, 2022 12:26 pm

I do not think there is an easy way, you can do something like this:
:foreach i in=$interfaces do={ :if ($i->"name" ~ "ether") do={
   do your thing here
}}

 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Searching multidimensional arrays

Mon May 02, 2022 2:08 pm

Something like this is ok?
{
    :local from "ether2"
    :local what "mtu"

    :local snapshot [:toarray ""]
    :set ($snapshot->"date") [/system clock get date]
    :set ($snapshot->"time") [/system clock get time]
    /interface
    :foreach if in=[find] do={
        :set (($snapshot)->[get $if name]) [get $if]
    }
    :put "On snapshot maded $($snapshot->"date") $($snapshot->"time")"
    :if ([:typeof (($snapshot)->$from)] != "nothing") do={
        :if ([:typeof (($snapshot)->$from)] != "nothing") do={
            :put "$from has $what set to $((($snapshot)->$from)->$what)"
        } else={
        :put "value $what for $from not found"
        }
    } else={
        :put "$from not found"
    }
}

Where the snapshot is maded have different results for example /interface and /interface ethernet:

terminal code

[rex@tended] /interface> get ether2 
actual-mtu    disabled    fp-rx-packet  l2mtu                link-downs   mtu      rx-byte   rx-packet  tx-drop    tx-queue-drop
comment       dynamic     fp-tx-byte    last-link-down-time  mac-address  name     rx-drop   slave      tx-error   type         
default-name  fp-rx-byte  fp-tx-packet  last-link-up-time    max-l2mtu    running  rx-error  tx-byte    tx-packet  value-name   

[rex@tended] /interface ethernet> get ether2 
advertise         combo-mode          disabled          fec-mode                   loop-protect-send-interval  name                rx-...
arp               comment             driver-rx-byte    full-duplex                loop-protect-status         not-learned         slave   
arp-timeout       current-learned     driver-rx-packet  l2mtu                      mac-address                 orig-mac-address    speed   
auto-negotiation  custom-drop-packet  driver-tx-byte    loop-protect               mdix-enable                 policy-drop-packet  switch  
bandwidth         default-name        driver-tx-packet  loop-protect-disable-time  mtu                         running             tx-...  

Who is online

Users browsing this forum: No registered users and 17 guests