Jump to content

Does this cast conversion make logical sense?


Recommended Posts

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

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 by VIP

Regards,
 

Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

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

  • Moderators

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

 

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

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

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png 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 columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

@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 :)

Code hard, but don’t hard code...

Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...