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.
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.
try remove “” from string
@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.
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.
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.
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:
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…