Community discussions

MikroTik App
 
sin3vil
newbie
Topic Author
Posts: 34
Joined: Sat May 26, 2018 10:05 pm

:tobool not working as expected

Thu Jun 13, 2019 5:41 pm

Hello,

Can someone explain why this isn't working?
[admin@MikroTik] > local string "true";local boolean [:tobool $string];put [typeof $boolean]
nil
Tested on 6.44.1 and 6.45beta27.
 
User avatar
ADahi
Member Candidate
Member Candidate
Posts: 209
Joined: Thu Sep 21, 2017 7:16 pm
Location: Iraq, Ninavah
Contact:

Re: :tobool not working as expected

Thu Jun 13, 2019 7:03 pm

try remove "" from string
 
User avatar
vecernik87
Forum Veteran
Forum Veteran
Posts: 882
Joined: Fri Nov 10, 2017 8:19 am

Re: :tobool not working as expected

Fri Jun 14, 2019 3:40 am

@ADahi: That is not a solution. He clearly wants to work with string. If you do
local string true;
, then you got variable named "string" containing boolean value. There would be no point in converting it to boolean if it already is boolean.

@sin3vil:
If you really require it to work with "true" and "false" strings, you can use condition:
local string "true";local boolean (string="true");put [typeof $boolean];put [$boolean]
I am not quite sure why your code does not work, but it seems that
:tobool
is really broken. I tried to use as parameter true/false, yes/no, 1/0 (both string or integer) and it does not produce any result. I can't figure out what possible input I need to use in order to get boolean result.
On the other hand, my proposed solution is actually shorter than your original idea - I guess no reason to use this conversion command at all.
 
User avatar
mrz
MikroTik Support
MikroTik Support
Posts: 7053
Joined: Wed Feb 07, 2007 12:45 pm
Location: Latvia
Contact:

Re: :tobool not working as expected

Fri Jun 14, 2019 1:22 pm

Currently :tobool does not work at all. If you could list all values that you would like to convert then we can try to implement it.
 
sin3vil
newbie
Topic Author
Posts: 34
Joined: Sat May 26, 2018 10:05 pm

Re: :tobool not working as expected

Tue Jun 25, 2019 11:39 am

Hello all,

Sorry, too a different path around this (string comparison instead of boolean) in my script and forgot about the thread.

I'm guessing any type of boolean operator should be translated by :tobool to true/false, so 1/0, yes/no, true/false.
 
User avatar
Amm0
Forum Guru
Forum Guru
Posts: 3451
Joined: Sun May 01, 2016 7:12 pm
Location: California

Re: :tobool not working as expected

Thu Dec 16, 2021 2:30 am

Currently :tobool does not work at all. If you could list all values that you would like to convert then we can try to implement it.
Actually ran into this... I thought it did work, but :tobool is just extremely conservative in what it converts. I did think it dealt with -1 as "false" – that isn't true, it's true. Very confusing difficult bug in one of my script... Anyway, why I write here so it's a little more clear how this works to me – my help to others.

I think this is right about "bool type:
:tobool only takes a [:typeof $x]="num", where only/exactly a num type of 0 is false, all other num are true. Thus, negative numbers, including -1, are also true.

The tri-state return (e.g. "nil"), especially for "-1", is the biggest issue here IMO since it means :tobool is "unsafe". :while (true) :for :toip it also returns nil, instead of say 0.0.0.0, for an invalid IP value provided to :toip...

I think this is the simplest script to show what's happening:
booltest=true using -1
booltest=false using 0
booltest=true using 1
booltest= using "1" and returns typeof nil
booltest=true using [tonum "1"] and returns typeof bool
from the following:
:global booltest [:tobool -1]
:put "booltest=$booltest using -1"
:global booltest [:tobool 0]
:put "booltest=$booltest using 0"
:global booltest [:tobool 1]
:put "booltest=$booltest using 1"
:global booltest [:tobool "1"]
:put "booltest=$booltest using \"1\" and returns typeof $([:typeof $booltest])"
:global booltest [:tobool [:tonum "1"]]
:put "booltest=$booltest using [tonum \"1\"] and returns typeof $([:typeof $booltest])"
My vote is a more liberal approach be more useful, since ROS uses many "synthetic bools" like "yes"/"no" in particular today:
  • if num, =<0 for false (thus >0 for true) - like the conditionals :while or :if
  • "yes"/"no", as used in "disabled=yes" etc
  • if string, "" means false, any text but "no" above is true
  • if nothing, false
  • if nil, nothing
  • if :ip, :ip6, others, true if "something" valid.
I'm sure others heavily using scripts have functions mask some of the weird type artifacts – i.e. looking at [:nothing], but see nil. Now I did learn [:tobool "1"] is one easy way to assign nil...

Who is online

Users browsing this forum: m3das and 35 guests