How to get multiple data using "value-name"

This script does print comment, address and host-name from all dhcp lease

/ip dhcp-server lease
:foreach i in=[find] do={
: put "$[get value-name=comment $i] $[get value-name=address $i] $[get value-name=host-name $i]"
}

Can this be shorten in any way?

I was looking for some like the example below

value-name=(comment|address|host-name)
value-name=comment,address,host-name
value-name="comment address host-name"

But this does not work.

Solved

:do {/ip dhcp-server lease
 :foreach i in=[find] do={
 :put ("comment=" . [get $i value-name=comment] . ", address=" . [get $i value-name=address] . ", host-name=" . [get $i value-name=host-name]);
 }
} on-error={}

This was even more code than what I have used. Gol was to do it more simple. (shorten)

How about this?

:foreach i in=[ print as-value ] do={
  :put (($i->"comment") . " " . ($i->"address") . " " . ($i->"host-name"));
}

Interesting and shorter.

Trying to understand how it works :wink:

To make sure it works in all location i would have used full command like this:

:foreach i in=[ /ip dhcp-server lease print as-value ] do={
  :put (($i->"comment") . " " . ($i->"address") . " " . ($i->"host-name"));
}

Usually I do. I started from your code, but did not notice the first line was appended to your written text. :laughing:

How can I make the results save to a file? My command prints the results on the terminal and creates the file but the file is empty.

:foreach i in=[ /ip/hotspot/user print file=aaaaaaaaaaa detail as-value where comment=admin ] do={:put (($i->“name”) . " " . ($i->“password”));}

I would like the results to be saved on the file also aside from just showing on the terminal.

:foreach i in=[ /ip/hotspot/user print file=aaaaaaaaaaa detail as-value where comment=admin ] do={:put ((“username: " . $i->“name”) .” " . "password: " . ($i->“password”));}

username: 649c8 password: 26927
username: dexsm password: 46789
username: 4btjb password: 93555
username: ua67a password: 97954
username: t5w6n password: 65495
username: rr2wm password: 25265
username: kwmd3 password: 73223
username: 9yexa password: 43636
username: bnkxv password: 84966
username: xrmpb password: 88595

DO NOT POST EVERYWHERE.

http://forum.mikrotik.com/t/scripting-displayed-results-save-to-file/148836/1

Short version from your example

/ip dhcp-s l
:fore i in=[f] do={:pu "$[g $i co] $[g $i address] $[g $i h]"}

Short version from your example

/ip dhcp-s l
:fore i in=[p as] do={:pu "$($i->"comment") $($i->"address") $($i->"host-name")"}

Joke apart

get value-name=comment $i
is equal to
get $i comment

and

“$(…) $(…) $(…)”
is better than
([…] . " " . […] . " " . […])


/ip dhcp-server lease
:foreach i in=[print as-value] do={:put "$($i->"comment") $($i->"address") $($i->"host-name")"}