len return wrong length

Hello,
i will post the leases to a webserver and ich must encode the string but len return always 1 with the following command:

:put [:len [/ip dhcp-server lease print as-value]]

Maybe someone has a solution?

best regards
Heiko

As @rextended pointed out some time back:
If you are using as-value in a script, you do some wrong.

It may be an better way, but this gives number of DHCP leases:

{
:local test 0
:foreach i in=[/ip dhcp-server lease find] do={
     :set $test ($test+1)
}
:put $test
}

Hi,

as-value returns an array of objects, it’s length is the total number of objects in the array.
You can use tostr on the whole thing to convert to a string. Or you can loop through the array selecting only the properties you need, like say the mac-address and build a string out of that.

put [len [tostr [/ip dhcp-server lease print as-value]]]
put [tostr [/ip dhcp-server lease print as-value]]

or

{
local string
foreach item in=[/ip dhcp-server lease print as-value] do={
set string ($string.($item->"mac-address").",")
}
#snip that last trailing comma
set string [pick $string 0 ([len $string]-1)]
put [len $string]
put $string
}

Please consider using the correct syntax, otherwise bad habits will spread

/ip dhcp-server lease
{
    :local string ""
    :foreach item in=[print as-value] do={
        :set $string "$string$($item->"mac-address"),"
    }
# snip that last trailing comma
    :set $string [:pick $string 0 ([:len $string] - 1)]
    :put [:len $string]
    :put $string
}

for me is better:

/ip dhcp-server lease
{
    :local string ""
    :foreach item in=[find] do={
        :set $string "$string,$[get $item mac-address]"
    }
# snip first trailing comma
    :set $string [:pick $string 1 [:len $string]]
    :put [:len $string]
    :put $string
}

Thanks…



can be… simplified:

:put [:len [/ip dhcp-server lease find]]

The “:len” return always 1 because you have only one entry on dhcp server leases?

For have the length of the string to pass at server, you must convert first the array returned from “print as-value” to one unique string
(As also wroted from @sin3vil):

:put [:len [:tostr [/ip dhcp-server lease print as-value] ] ]

If you have more than one DHCP server on your Router.

/ip dhcp-server 
:foreach i in=[find] do={
     :local name [get $i name]
     :local number [:len [lease find where server=$name]]
     :put "scope=$name leases=$number"
}