Jump to content

noob question ('string' = 0)


JohnOne
 Share

Recommended Posts

I apologise for this question as I'm sure I have seen it before but have simply forgotten.

I had it in my head that only an empty string was equal to 0

Also that to test a case string is ==

But I'm unsure if this behaviour is normal.

$string = 'string'
If $string = 0 Then
   MsgBox(0,0,$string)
EndIf

It results in a message box, I cannot remember why.

Can someone refresh my memory, as it is difficult to search.

Edited by JohnOne

AutoIt Absolute Beginners    Require a serial    Pause Script    Video Tutorials by Morthawt   ipify 

Monkey's are, like, natures humans.

Link to comment
Share on other sites

  • Moderators

JohnOne,

In a Boolean comparison, an empty string converts to False, if it has any content then it converts to True. For numbers 0 = False, anything else = True.

Using == forces both sides of a comparison into string type and makes a case-sensitive comparison.

It is best to use the same type on both sides of a comparison - AutoIt does its best to guess what you want but it is best to make it explicit. :)

I am not sure why you get a MsgBox from the code you posted - I would not have expected one at first glance. I can only imagine that there is some type changing going on behind the scenes and the result is not what you expect. But as the 2 sides of the comparison are currently different types, my advice above is relevant - use the same type for both: ;)

$string = "string"
If $string = "" Then
    MsgBox(0, 0, "empty string")
Else
    MsgBox(0, 0, $string)
EndIf

That works as I expect in both cases. ;)

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

Only an empty string ("") will be a Boolean false
Any other string values (including a string equal "0") will be a Boolean true

Seems to be a bug, especially since case sensitive comparison yields expected results. however, in strictly boolean tests:

If $string = false  Then

or

If not $string Then

works as expected. In any case, you should never compare strings to numbers, its undefined in most cases

Ever wanted to call functions in another process? ProcessCall UDFConsole stuff: Console UDFC Preprocessor for AutoIt OMG

Link to comment
Share on other sites

I apologise for this question as I'm sure I have seen it before but have simply forgotten.

I had it in my head that only an empty string was equal to 0

Also that to test a case string is ==

But I'm unsure if this behaviour is normal.

$string = 'string'
If $string = 0 Then
   MsgBox(0,0,$string)
EndIf

It results in a message box, I cannot remember why.

Can someone refresh my memory, as it is difficult to search.

Because numeric value of string 'string' is 0.

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

  • Moderators

trancexx,

Just out of interest, and certainly not because I intend ever using it, is there a simple logic behind AutoIt's type use for comparisons? I was thinking along the lines of numeric 0 being converted to the literal string "0", but you indicate that it was the string being converted to a number. Which side gets the priority in a mixed type comparison? :)

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

trancexx,

Just out of interest, and certainly not because I intend ever using it, is there a simple logic behind AutoIt's type use for comparisons? I was thinking along the lines of numeric 0 being converted to the literal string "0", but you indicate that it was the string being converted to a number. Which side gets the priority in a mixed type comparison? :)

M23

Side doesn't matter, operator does. If you do "==" then it's all about strings, but if you use "=" then AutoIt uses table to determine how to do a job.

Don't worry, it does it in most logical way.

$string = '1string'
If $string = 1 Then
MsgBox(0, 0, $string)
EndIf

♡♡♡

.

eMyvnE

Link to comment
Share on other sites

  • Moderators

trancexx,

it does it in most logical way

Based on whose definition of logic, I wonder? ;)

Thanks for responding. :)

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

I tried explaining why this happens some time back. In autoit, if a string is a number AND it is used with or compared to a number, a implicit call to Number() function will be made on the string. If the string doesn't contain a valid number, it will be assumed to equal to 0.

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...