Community discussions

MikroTik App
 
jeroenp
Member Candidate
Member Candidate
Topic Author
Posts: 159
Joined: Mon Mar 17, 2014 11:30 am
Location: Amsterdam
Contact:

On `get` vs `print` on objects and the inequality of how empty string properties are handled: why is that?

Tue May 24, 2016 9:55 pm

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 `get` and `print` behave 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-name` is not present when empty
- `comment` is 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
==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
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 6110
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: On `get` vs `print` on objects and the inequality of how empty string properties are handled: why is that?

Wed May 25, 2016 10:09 am

Some parameters can be unset. If parameter is unset it does not exist so it is not printed. For example comment cannot be unset, you can set it to empty string so it shows always when printed.

Print command can have several parameters to print for example read only properties, for example, print stats, print status, print detail.
 
jeroenp
Member Candidate
Member Candidate
Topic Author
Posts: 159
Joined: Mon Mar 17, 2014 11:30 am
Location: Amsterdam
Contact:

Re: On `get` vs `print` on objects and the inequality of how empty string properties are handled: why is that?

Wed May 25, 2016 11:01 pm

Some parameters can be unset. If parameter is unset it does not exist so it is not printed. For example comment cannot be unset, you can set it to empty string so it shows always when printed.
Where do I find which proeprties can be unset?
Print command can have several parameters to print for example read only properties, for example, print stats, print status, print detail.
Is there a `print` parameter to show all read-only properties in addition to the read-write properties?

Thanks for the input so far.

--jeroen

Who is online

Users browsing this forum: No registered users and 18 guests