Community discussions

MikroTik App
 
rajo
newbie
Topic Author
Posts: 39
Joined: Tue Aug 16, 2011 11:12 pm

Impossible to retrieve array key names with 6.4

Fri Oct 04, 2013 4:20 am

I have a script that can monitor any UPS. I alert on only certain parameters; however, I report all parameters.

With 5.x, I used a foreach loop to access known key/value pairs. With 6.4, I had to change that to something like this:
:set OnLine ($upsstat -> "on-line")
:set LineVoltage ($upsstat -> "line-voltage")
For unknown keys, I used a foreach loop in 5.x; however, there is no way in 6.4 for me to retrieve either an unknown key or key/value pair. In 5.x, something like this:
:foreach temp in=[/system ups monitor [/system ups get [/system ups find name~".*"] name] once as-value]  do={ :put $temp }
would produce the output:
.id=*1
on-line=true
runtime-left=01:20:03
battery-charge=100
line-voltage=12200
output-voltage=12200
load=3
And the following (on 5.x):
:foreach temp in=[/system ups monitor [/system ups get [/system ups find name~".*"] name] once as-value]  do={ :put [:pick $temp 0 [/find $temp "="]] }
would produce the output:
.id
on-line
runtime-left
battery-charge
line-voltage
output-voltage
load
In 5.x, if I do:
:put [:pick [/system ups monitor [/system ups get [/system ups find name~".*"] name] once as-value] 2]
I get:
runtime-left=01:20:03
But with 6.4, I get nothing. I'm forced to know the key in advance -- something I will not always know, since a UPS in the field may be plugged in and report parameters my script must process, but which I will not necessarily know in advance. With 5.x, my script automatically adapts to whatever UPS is plugged in; however, this can only work properly if I can retrieve the array key names and not just the values.

I attempted to workaround this by converting the array to a string and then searching the string, but this is impossible with 6.4. You cannot convert an array to a string with 6.4. You get a blank. Specifically, the following does not work in 6.4:
:put [:tostr [/system ups monitor [/system ups get [/system ups find name~".*"] name] once as-value]]
I need a solution to this, as this is preventing me from using 6.4 for a certain deployments.

Thanks
 
User avatar
boen_robot
Forum Guru
Forum Guru
Posts: 2411
Joined: Thu Aug 31, 2006 4:43 pm
Location: europe://Bulgaria/Plovdiv

Re: Impossible to retrieve array key names with 6.4

Fri Oct 04, 2013 11:42 am

@MikroTik

As a solution to the above problem (and a few related others) I propose a new command (in this example called ":keys", but feel free to name it differently) that would return an indexed array, with values that are the key at the corresponding numeric index of the original array.

(note: numeric indexes from the original array should have "nil" as a value in :keys, but be present; this assumes "nil" is not a valid key*)

e.g.

ros code

:local arr {1=33;a=true;b=10d00:00:01;"test1";"test2"}
:local arrKeys [:keys $arr]
:put arr
:put arrKeys
:put [:pick arr 2]
:put [:pick arrKeys 2]
should output
test1;test2;1=33;a=true;b=1w3d00:00:01
;;1;a;b
33
1
* Speaking of which, using a boolean, IP or a time value as a key does not cause syntax errors, which would be perfectly OK if these values were allowed, except they behave weirdly. Using a boolean "true" as a key moves the member to the end of the array (after both numeric keys and other keys), using a time value as a key causes a single member with a boolean "false" to be created, and IP is put somewhere in the middle by who knows what criteria.
PEAR2_Net_RouterOS(1.0.0b6) - My API client in PHP
(Rate my posts? If you want... no pressure...)

Who is online

Users browsing this forum: gorec2005, YaCy [Bot] and 19 guests