Community discussions

MikroTik App
 
mikrotiklhp
just joined
Topic Author
Posts: 2
Joined: Mon Jul 05, 2021 7:58 pm

Extract item from array (or whatever works)

Tue Jul 06, 2021 12:45 am

Hello. Looking for a way to give the "dhcphosts" variable the value, as num, with actual dynamic dhcp clients number on the system, so I can do something like: (if dhcphosts != 0, do something).
"dhcphosts" stores the value as an array as follows, any idea on how to achieve this?. I need to go from being an array to a number for the variable.

#Variable assignment
[MikroTik] > :global dhcphosts do={ip dhcp-server lease print detail count-only where dynamic}
#Check variable value and type
[MikroTik] > $dhcphosts
1
[MikroTik] > :put [$dhcphosts ]
1
;1
[MikroTik] > :typeof [$dhcphosts ]
1
[MikroTik] > :put [:typeof [$dhcphosts ]]
1
array
[MikroTik] > :put [:typeof ($dhcphosts )]
array
 
User avatar
eworm
Forum Guru
Forum Guru
Posts: 1071
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: Extract item from array (or whatever works)

Wed Jul 07, 2021 1:47 am

You want the number of dynamic dhcp leases? Try this:
:global dhcphosts [ :len [ / ip dhcp-server lease find where dynamic ] ];
 
mikrotiklhp
just joined
Topic Author
Posts: 2
Joined: Mon Jul 05, 2021 7:58 pm

Re: Extract item from array (or whatever works)

Wed Jul 07, 2021 2:41 am

eworm, you are a genius, thank you.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12001
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Extract item from array (or whatever works)

Wed Jul 07, 2021 3:26 am

:global dhcphosts [ :len [ / ip dhcp-server lease find where dynamic ] ];

Sincerely I prefer to be explicit, if for some reason on future that "dynamic" change behavior...
:global dhcphosts [:len [/ip dhcp-server lease find where dynamic=yes]]
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 12001
Joined: Tue Feb 25, 2014 12:49 pm
Location: Italy
Contact:

Re: Extract item from array (or whatever works)

Wed Jul 07, 2021 3:30 am

[MikroTik] > :global dhcphosts do={ip dhcp-server lease print detail count-only where dynamic}
on that way you write a function to count everityme launched the number of dynamic leases

With your method (except for the detail of useless print the number on the terminal) you were almost close to the solution for write directly the value inside a variable:
:global dhcphosts [/ip dhcp-server lease print count-only where dynamic=yes]

and about:
:typeof [$dhcphosts ]
when you call a variable, do not use the [ ] !
any results on terminal must be see with :put
you must write like
:put $dhcphosts
:put [:typeof $dhcphosts]

Who is online

Users browsing this forum: Gomo, txfz and 57 guests