Inspired by some boen_robot code, I’ve been playing around with get and print to get properties from an object as array so they can be dereferenced using the -> operator. I think this is better readable than repeated get statements for each property, but might also be faster.
I’m wondering:
- why do
getandprintbehave differently? - why are some string properties sometimes absent when empty (but are other string properties always present even when empty)?
get gives you more properties because you also get read-only properties some string properties can be absent when they don’t have a value, but this doesn’t hold for all properties: for instance
host-nameis not present when emptycommentis present when empty
{
:foreach leaseId in [/ip dhcp-server lease find] do={
:local lease [/ip dhcp-server lease get $leaseId];
:put $lease;
:local leaseAddress ($lease->"address")
:local leaseMac ($lease->"mac-address")
:put "$leaseMac -> $leaseAddress"
}
}
print gives you less members, but skips one level of indirection and since the members (apart from .id) seem to be read-write can be used for saving/restoring state.
{
:local leases [/ip dhcp-server lease print detail as-value];
:foreach lease in=$leases do={
:put $lease
:local leaseAddress ($lease->"address")
:local leaseMac ($lease->"mac-address")
:put "$leaseMac -> $leaseAddress"
}
}
Most of my code started out as multiple gets which requires less indirection and less quotes but longer statements and prevents passing all properties at one to methods.
{
:global varDump
:foreach leaseId in [/ip dhcp-server lease find] do={
:local leaseAddress [/ip dhcp-server lease get $leaseId address];
:local leaseMac [/ip dhcp-server lease get $leaseId mac-address];
:put "$leaseMac -> $leaseAddress"
}
}
Differences between get and print:
GET
==GET===========================================================================
#-STATIC-with-HOST-NAME---------------------------------------------------------
.id=*c1;active-address=192.168.71.23;active-client-id=1:f0:de:f1:37:19:a2;active-mac-address=F0:DE:F1:37:19:A2;active-server=dhcp_lan;address=192.168.71.23;address-lists=;blocked=false;comment=w701ujpl;dhcp-option=;disabled=false;dynamic=false;expires-after=00:09:29;host-name=W701UJPL;last-seen=31s;mac-address=F0:DE:F1:37:19:A2;radius=false;server=dhcp_lan;status=bound
#-DYNAMIC-without-HOST-NAME-----------------------------------------------------
.id=*df;active-address=192.168.71.20;active-client-id=1:0:80:92:5f:59:27;active-mac-address=00:80:92:5F:59:27;active-server=dhcp_lan;address=192.168.71.20;address-lists=;blocked=false;client-id=1:0:80:92:5f:59:27;dhcp-option=;disabled=false;dynamic=true;expires-after=00:01:03;last-seen=8m57s;mac-address=00:80:92:5F:59:27;radius=false;server=dhcp_lan;status=bound
==PRINT=========================================================================
#-STATIC-with-HOST-NAME---------------------------------------------------------
.id=*c1;active-address=192.168.71.23;active-client-id=1:f0:de:f1:37:19:a2;active-mac-address=F0:DE:F1:37:19:A2;active-server=dhcp_lan;address=192.168.71.23;address-lists=;comment=w701ujpl;dhcp-option=;expires-after=00:05:25;host-name=W701UJPL;last-seen=4m35s;mac-address=F0:DE:F1:37:19:A2;server=dhcp_lan;status=bound
#-DYNAMIC-without-HOST-NAME-----------------------------------------------------
.id=*df;active-address=192.168.71.20;active-client-id=1:0:80:92:5f:59:27;active-mac-address=00:80:92:5F:59:27;active-server=dhcp_lan;address=192.168.71.20;address-lists=;client-id=1:0:80:92:5f:59:27;comment=;dhcp-option=;expires-after=00:06:03;last-seen=3m57s;mac-address=00:80:92:5F:59:27;server=dhcp_lan;status=bound
–jeroen