Community discussions

MikroTik App
 
mike548141
just joined
Topic Author
Posts: 23
Joined: Sun Aug 16, 2020 5:14 am

print count-only without printing to console

Tue Jun 15, 2021 5:37 am

Hi

I use "print count-only where" in scripts quite a bit (example below) as its cleaner than using :len. The down side is I get unwanted integers printed to the console which are the counts.
Is there a way that I can use a count only without printing the result to console? The only way I can think of is to capture the output of the print count only in a variable first, before runnign the if statement.

Example
:if ([ /user print count-only where name=$usercn; ] = 0) do={
      :put ("Creating the user " . $usercn);
      /user add name=$usercn group=$group address=$address password=$password comment=$comment;
    };
Cheers,

MC
 
User avatar
eworm
Forum Veteran
Forum Veteran
Posts: 803
Joined: Wed Oct 22, 2014 9:23 am
Location: Oberhausen, Germany
Contact:

Re: print count-only without printing to console

Wed Jun 16, 2021 1:20 am

You can not be silent with print count-only. Use :len if you want.
Manage RouterOS scripts and extend your devices' functionality: RouterOS Scripts
For contact join the RouterOS-Scripts Telegram group!
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 4808
Joined: Tue Feb 25, 2014 12:49 pm
Location: Capalbio, Tuscany, Italy

Re: print count-only without printing to console

Wed Jun 16, 2021 1:54 am

more clear:
:if ([:len [/user find where name=$usercn]] = 0) do={
    :put "Creating the user $usercn"
    /user add name=$usercn group=$group address=$address password=$password comment=$comment
}
Last edited by rextended on Wed Jun 16, 2021 1:58 am, edited 1 time in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 4808
Joined: Tue Feb 25, 2014 12:49 pm
Location: Capalbio, Tuscany, Italy

Re: print count-only without printing to console

Wed Jun 16, 2021 1:57 am

...its cleaner than using :len...
...The down side is I get unwanted integers printed to the console which are the counts...
At this point is clearner using :len because the downside print on the console...

:if ([ /user print count-only where name=$usercn; ] = 0) do={
traduction: if the print with count oly the results of all user with name equal to the variable on $usercn is 0...

:if ([:len [/user find where name=$usercn]] = 0) do={
traduction: if the lenght of the search of all user with name equal to the variable on $usercn is 0...

Which is clearer of the two?
 
mike548141
just joined
Topic Author
Posts: 23
Joined: Sun Aug 16, 2020 5:14 am

Re: print count-only without printing to console

Wed Jun 16, 2021 2:41 am

Thanks for the feedback/answers @eworm & @rextended, I'm not surprised its not possible to make the count-only silent but I was hoping that someone had a way.
Maybe its just me, but to me the print count-only is cleaner code to read, and requires fewer CPU cycles & memory than a :len to execute (assuption, not measured). I take from your replys that you think :len is cleaner code, obviosuly its cleaner on the UI as its not spouting unwanted output.

I think of the "print count-only where" like an SQL query that filters and aggregates it at source, minimising the impact on the database, the comms channel, and the consuming app. Rather than an open SQL query that retireves a large dataset and then filters and aggregates post (at the consuming app).
Of course in this instance its tiny amounts and so it doesnt matter so much, but a principle I use irrespective of the language/platform (I'm just learning RouterOS scripting).
...
...
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 4808
Joined: Tue Feb 25, 2014 12:49 pm
Location: Capalbio, Tuscany, Italy

Re: print count-only without printing to console

Wed Jun 16, 2021 2:51 am

... but to me the print count-only is cleaner code to read, and requires fewer CPU cycles & memory than a :len to execute (assuption, not measured)...
Think about: Just because print something on screen use twice the CPU

print / count suppose than one SEARCH (find) is made, and are counted only the LENgth of thesult,

len /find suppose than one SEARCH (find) is made, and are counted only the LENgth of thesult,

oh, no apaprent computational difference...

but... print still also.. print on video
Last edited by rextended on Wed Jun 16, 2021 2:59 am, edited 2 times in total.
 
User avatar
rextended
Forum Guru
Forum Guru
Posts: 4808
Joined: Tue Feb 25, 2014 12:49 pm
Location: Capalbio, Tuscany, Italy

Re: print count-only without printing to console

Wed Jun 16, 2021 2:54 am

...but to me the print count-only is cleaner code to read...
For me, when I SEARCH (or count) something, FIND is more close to the concept instead of PRINT something on video.

When I go to the market, I must PRINT my favorite juice...

Who is online

Users browsing this forum: No registered users and 13 guests