Community discussions

MikroTik App
 
AKCErftstadt
just joined
Topic Author
Posts: 7
Joined: Thu Nov 17, 2011 10:49 pm
Location: Germany - Cologne

len return wrong length

Fri Nov 12, 2021 1:50 pm

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
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: len return wrong length

Fri Nov 12, 2021 2:34 pm

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
}
 
sin3vil
newbie
Posts: 34
Joined: Sat May 26, 2018 10:05 pm

Re: len return wrong length

Wed Nov 17, 2021 10:05 am

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
}
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: len return wrong length

Wed Nov 17, 2021 11:32 am

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
}
Last edited by rextended on Wed Nov 17, 2021 12:19 pm, edited 3 times in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: len return wrong length

Wed Nov 17, 2021 12:02 pm

As @rextended pointed out some time back:
Thanks....


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

can be... simplified:
:put [:len [/ip dhcp-server lease find]]
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 11982
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: len return wrong length

Wed Nov 17, 2021 12:39 pm

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

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] ] ]
 
User avatar
Jotne
Forum Guru
Forum Guru
Posts: 3291
Joined: Sat Dec 24, 2016 11:17 am
Location: Magrathean

Re: len return wrong length

Wed Nov 17, 2021 1:16 pm

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"
}

Who is online

Users browsing this forum: Google [Bot] and 20 guests