Sign in to follow this  
Followers 0
illuminon

wired: $someString = 0 is always true

11 posts in this topic

Hi there!

Some wired behavior (bug) of autoit3 cost me considerable time to debug. Here the core of the problem:

$string = "b"
If $string = 0 Then
MsgBox(0,"","weird, string is " & $string)
Else
MsgBox(0,"","all ok, string is " & $string)
EndIf

This shows: "weird, string is b"

Is this intentionally? I would have expected this statement to be false or at least cast some datatype error. But it seems to be true. The issue is solved when

$string == 0

is used. This behavior is devastating when for instance searching a value in a string array which has occasional gaps filled with zeroes by a data acquisition machine.

Any comments on this observation?

cheers

Share this post


Link to post
Share on other sites



illuminon,

Although not a Dev, I believe that you are running into a conflict of datatypes here. Remember that AutoIt has only one datatype - "Variant" - and so it is up to you to make sure you do the correct match.

In the case you post, you are asking AutoIt if a string variable is 0 - that means False or "". As the variable is not "", you get the result you do NOT expect. :graduated:

If you were to use If $string = "0", you would get the expected response as the 2 strings would not match.

Having only a single datatype allows fantastic flexibility and is, in the opinion of most experienced users, worth the odd occasion where you need to take extra care - as here.

All clear? ;)

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______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

 

Share this post


Link to post
Share on other sites

Hi Melba

Having learned the ropes of programming with C and C++ I fully agree concerning the advantages of a single datatype. However, I ask whether $string is 0, or in other words, as you say, whether $string is "". Since it is not empty, it's "b", I expect the expression to be false and not true. As far as I understand your reasoning it still should be what I expected, no?

Share this post


Link to post
Share on other sites

Wow this is kinda weird.

This returns all ok... :graduated:

$string = "b"
If Not $string Then
MsgBox(0, "", "weird, string is " & $string)
Else
MsgBox(0, "", "all ok, string is " & $string)
EndIf

Share this post


Link to post
Share on other sites

When you try to compare a string to a 1 or 0 the string is converted to a number before it's compared, and strings evaluate to 0.

From the help file:

If a string is used as a number, an implicit call to Number() function is done. So if it doesn't contain a valid number, it will be assumed to equal 0.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

But that says if a string is used AS a number. Not compared to a number. Its referring to something like this:

$string = '123'
If $string = 123 then MsgBox(0,'', 'String is 123')
Edited by Beege

Share this post


Link to post
Share on other sites

When you do a boolean compare with a string, it's either 1 or 0 (True or False), so it's comparing a string to a number. The keyword True is considered anything that's not 0, and a boolean compare is done the same way. Also from the same section of the help file:

If you use strings together with Boolean values, they will be converted as follows:

A Boolean true will be the string value "True"

A Boolean false will be the string value "False"

Example:

$Boolean1=true

$String1="Test is: "

$String2=$String1 & $Boolean1

This will result in $String2 being the string value "Test is: True"

The other way around however is different. When you use string comparisons with Boolean values, the following rules apply:

Only an empty string ("") will be a Boolean false

Any other string values (including a string equal "0") will be a Boolean true


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

Any other string values (including a string equal "0") will be a Boolean true

Now thats the key statement I was looking for.

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Damn it... that statement didnt do it for me. So like you just, it's either 1 or 0 (True or False). So with that logic I always thought $string=0 and $string=false would evaluate the same. Same thing with the NOT keyword. Before now I thought "if $string=0" and "If Not $string" would return the same, but they dont.

$string = "b"
If $string = True Then
MsgBox(0,'','True')
EndIf
 
If $string = 1 Then
MsgBox(0,'','1')
EndIf
 
If $string = False Then
MsgBox(0,'', 'False')
EndIf
 
If $string = 0 Then
MsgBox(0,'', 'Zero')
EndIf
 
if Not $string then
MsgBox(0,'', 'not')
EndIf
 
if $string then
MsgBox(0,'', 'if $string')
EndIf
Edited by Beege

Share this post


Link to post
Share on other sites

That was enlightening :-) Thanks for untangelling,

cheers

Share this post


Link to post
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
Sign in to follow this  
Followers 0