JockoDundee Posted May 5, 2021 Share Posted May 5, 2021 Local $a=True, $b=1, $c="A" If $a=$b Then ConsoleWrite("$a=$b" &@CRLF) If $b=$c Then ConsoleWrite("$b=$c" &@CRLF) If $c=$a Then ConsoleWrite("$c=$a" &@CRLF) When I run this I get an unexpected result (for me). Apparently, AutoIt, when converting between strings and booleans and integers does this: 1) converts empty strings to False and 0 2) converts non-empty strings to True and 0 Am I correct in this assertion? Code hard, but don’t hard code... Link to comment Share on other sites More sharing options...
Trong Posted May 6, 2021 Share Posted May 6, 2021 (edited) Autoit Boolean values: True <> 0 AND non-empty Strings False = Empty string = Null = 0 _Comparison(True, 1) _Comparison(1, True) _Comparison(-1, True) _Comparison(0, True) _Comparison(5, True) _Comparison(1, 'A') _Comparison('A', True) _Comparison('DSFGSDFG', True) _Comparison('SDF', True) _Comparison(True, '') _Comparison(True, 'X') _Comparison('A', 'a') Func _Comparison($x, $y) If ($x == $y) Then ConsoleWrite("-1: " & $x & ' == ' & $y & @CRLF) Return 1 ElseIf ($x = $y) Then ConsoleWrite("-1: " & $x & ' = ' & $y & '' & @CRLF) Return -1 Else ConsoleWrite("-0: " & $x & ' <> ' & $y & @CRLF) Return 0 EndIf EndFunc ;==>_Compariso https://www.autoitscript.com/autoit3/docs/intro/lang_operators.htm Quote = Tests if two values are equal. e.g. If $vVar = 5 Then (true if $vVar equals 5). Case-insensitive when used with strings. See below about comparing with mixed datatypes. == Tests if two strings are equal. Case-sensitive. The left and right values are converted to strings if they are not strings already. This operator should only be used if string comparisons need to be case-sensitive. <> Tests if two values are not equal. Case-insensitive when used with strings. To do a case-sensitive not equal comparison use Not ("string1" == "string2") Edited May 6, 2021 by VIP JockoDundee 1 Regards, Link to comment Share on other sites More sharing options...
JockoDundee Posted May 6, 2021 Author Share Posted May 6, 2021 10 hours ago, VIP said: True <> 0 AND non-empty Strings False = Empty string = Null = 0 Right! But since non-empty Strings = 0 therefore True = False ? Code hard, but don’t hard code... Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted May 6, 2021 Moderators Share Posted May 6, 2021 JockoDundee, The vital part of the "Operators" page in the Help file is further down than VIP's quote above: Quote Comparing different datatypes Care is needed if comparing mixed datatypes, as unless the case-sensitive (==) string operator is used, mixed comparisons are usually made numerically. Most strings will be evaluated as 0 and so the result may well not be the one expected. It is recommended to force the items being compared into the same datatype using Number()/String() before the comparison. So your statement that both empty AND non-empty strings are converted to 0 is quite true - and why it is very inadvisable to mix datatyes in comparisons. M23 Danp2, JockoDundee and TheDcoder 3 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
JockoDundee Posted May 6, 2021 Author Share Posted May 6, 2021 1 hour ago, Melba23 said: So your statement that both empty AND non-empty strings are converted to 0 is quite true... In that case, I suppose I'm wondering why both empty AND non-empty strings are not converted to False... 1 hour ago, Melba23 said: and why it is very inadvisable to mix datatyes in comparisons. So is this usage common usage inadvisable? Local $str="ABC" If $str Then ... Btw, here's a brain twister - Guess the output just by sight: Local $a="abc" If $a=1 Then ConsoleWrite("$a=1" &@CRLF) If Not $a=1 Then ConsoleWrite("Not $a=1" &@CRLF) If Not Not $a=1 Then ConsoleWrite("Not Not $a=1" &@CRLF) Thanks for your insight... Code hard, but don’t hard code... Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted May 6, 2021 Moderators Share Posted May 6, 2021 JockoDundee, Quote I'm wondering why both empty AND non-empty strings are not converted to False.. Read the explanation again. Mixed datatype comparisons are usually forced to numeric - and strings which do not start with numeric characters will then almost certainly be converted to 0. When it comes to Booleans things get a bit more complex - read the Help file datatypes page (particularly the <Boolean> section) to see how much. Composing that section took me a lot of time before I found a formulation which I hoped was understandable! Quote So is this usage common usage inadvisable? No, because in this case the string is converted to a Boolean - the Help file page explains how it is done internally. However, I would much prefer: Local $str="ABC" If $str <> "" Then ... so that the string nature of both sides of the comparison is explicit. Quote here's a brain twister I am not even going to try - using Not without brackets to delimit the argument is always fraught with danger and can lead to all sorts of chaos.. Top Tip - do NOT mix datatypes in comparisons! M23 FrancescoDiMuro and JockoDundee 2 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
JockoDundee Posted May 6, 2021 Author Share Posted May 6, 2021 11 minutes ago, Melba23 said: I am not even going to try The tricky (for me) thing is that neither the line $a=1 or the Not $a=1 print. Code hard, but don’t hard code... Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted May 6, 2021 Moderators Share Posted May 6, 2021 JockoDundee: Local $a = "abc" ; Not True as $a converts to 0 If $a = 1 Then ConsoleWrite("$a=1" & @CRLF) ; Not true as $a converts to True, Not $a toggles to False, and 1 is converted to boolean True If (Not $a) = 1 Then ConsoleWrite("(Not $a) = 1" & @CRLF) ; True as ($a = 1) is False (see above), and Not then toggles it to True If Not ($a = 1) Then ConsoleWrite("Not ($a = 1)" & @CRLF) ; I leave these as exercises for the student - as my old maths master used to say If Not (Not $a) = 1 Then ConsoleWrite("Not (Not $a) = 1" & @CRLF) If Not (Not ($a = 1)) Then ConsoleWrite("Not (Not ($a = 1))" & @CRLF) M23 Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind Open spoiler to see my UDFs: Spoiler ArrayMultiColSort ---- Sort arrays on multiple columnsChooseFileFolder ---- Single and multiple selections from specified path treeview listingDate_Time_Convert -- Easily convert date/time formats, including the language usedExtMsgBox --------- A highly customisable replacement for MsgBoxGUIExtender -------- Extend and retract multiple sections within a GUIGUIFrame ---------- Subdivide GUIs into many adjustable framesGUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView itemsGUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeViewMarquee ----------- Scrolling tickertape GUIsNoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxesNotify ------------- Small notifications on the edge of the displayScrollbars ----------Automatically sized scrollbars with a single commandStringSize ---------- Automatically size controls to fit textToast -------------- Small GUIs which pop out of the notification area Link to comment Share on other sites More sharing options...
JockoDundee Posted May 9, 2021 Author Share Posted May 9, 2021 @Melba23, Local $a = "abc" ; Not True as $a converts to 0 If $a = 1 Then ConsoleWrite("$a=1" & @CRLF) ; Not true as $a converts to True, Not $a toggles to False, and 1 is converted to boolean True If (Not $a) = 1 Then ConsoleWrite("(Not $a) = 1" & @CRLF) ; True as ($a = 1) is False (see above), and Not then toggles it to True If Not ($a = 1) Then ConsoleWrite("Not ($a = 1)" & @CRLF) ; I leave these as exercises for the student - as my old maths master used to say ; Funny thats what my old gym teacher used to say ; True as (Not $a) is False, Not toggles to True, and 1 is converted to boolean True If Not (Not $a) = 1 Then ConsoleWrite("Not (Not $a) = 1" & @CRLF) ; Not True as ($a = 1) is False and Not toggles True and Not toggles False If Not (Not ($a = 1)) Then ConsoleWrite("Not (Not ($a = 1))" & @CRLF) btw, and afaik, unlike some other basic Is<data type> functions, IsNumber(), IsString(), IsInt(), IsPtr(), with IsBool() there is no complimentary cast function to Boolean, e.g. Bool(). This may be by design because of limited use, or perhaps some other reason. In any case, if at any time a decision is made to add such a function to the canon, please consider the following implementation: Func Bool($vExpression) Return Not Not $vExpression EndFunc (Please no “Not Not” Jokes from the peanut gallery TheDcoder 1 Code hard, but don’t hard code... Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now