illuminon Posted September 14, 2011 Share Posted September 14, 2011 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 Link to comment Share on other sites More sharing options...
Moderators Melba23 Posted September 14, 2011 Moderators Share Posted September 14, 2011 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. 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 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...
illuminon Posted September 14, 2011 Author Share Posted September 14, 2011 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? Link to comment Share on other sites More sharing options...
Beege Posted September 14, 2011 Share Posted September 14, 2011 Wow this is kinda weird. This returns all ok... $string = "b" If Not $string Then MsgBox(0, "", "weird, string is " & $string) Else MsgBox(0, "", "all ok, string is " & $string) EndIf Assembly Code: fasmg . fasm . BmpSearch . Au3 Syntax Highlighter . Bounce Multithreading Example . IDispatchASMUDFs: Explorer Frame . ITaskBarList . Scrolling Line Graph . Tray Icon Bar Graph . Explorer Listview . Wiimote . WinSnap . Flicker Free Labels . iTunesPrograms: Ftp Explorer . Snipster . Network Meter . Resistance Calculator Link to comment Share on other sites More sharing options...
BrewManNH Posted September 15, 2011 Share Posted September 15, 2011 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 GudeHow 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 Link to comment Share on other sites More sharing options...
Beege Posted September 15, 2011 Share Posted September 15, 2011 (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 September 15, 2011 by Beege Assembly Code: fasmg . fasm . BmpSearch . Au3 Syntax Highlighter . Bounce Multithreading Example . IDispatchASMUDFs: Explorer Frame . ITaskBarList . Scrolling Line Graph . Tray Icon Bar Graph . Explorer Listview . Wiimote . WinSnap . Flicker Free Labels . iTunesPrograms: Ftp Explorer . Snipster . Network Meter . Resistance Calculator Link to comment Share on other sites More sharing options...
BrewManNH Posted September 15, 2011 Share Posted September 15, 2011 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 & $Boolean1This 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 falseAny 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 GudeHow 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 Link to comment Share on other sites More sharing options...
Beege Posted September 15, 2011 Share Posted September 15, 2011 Any other string values (including a string equal "0") will be a Boolean true Now thats the key statement I was looking for. Assembly Code: fasmg . fasm . BmpSearch . Au3 Syntax Highlighter . Bounce Multithreading Example . IDispatchASMUDFs: Explorer Frame . ITaskBarList . Scrolling Line Graph . Tray Icon Bar Graph . Explorer Listview . Wiimote . WinSnap . Flicker Free Labels . iTunesPrograms: Ftp Explorer . Snipster . Network Meter . Resistance Calculator Link to comment Share on other sites More sharing options...
Beege Posted September 15, 2011 Share Posted September 15, 2011 (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 September 15, 2011 by Beege Assembly Code: fasmg . fasm . BmpSearch . Au3 Syntax Highlighter . Bounce Multithreading Example . IDispatchASMUDFs: Explorer Frame . ITaskBarList . Scrolling Line Graph . Tray Icon Bar Graph . Explorer Listview . Wiimote . WinSnap . Flicker Free Labels . iTunesPrograms: Ftp Explorer . Snipster . Network Meter . Resistance Calculator Link to comment Share on other sites More sharing options...
illuminon Posted September 15, 2011 Author Share Posted September 15, 2011 That was enlightening :-) Thanks for untangelling, cheers Link to comment Share on other sites More sharing options...
ChrisL Posted September 15, 2011 Share Posted September 15, 2011 Your after If $String= "" then ;String is blank Else ;String is not blank Endif [u]Scripts[/u]Minimize gui to systray _ Fail safe source recoveryMsgbox UDF _ _procwatch() Stop your app from being closedLicensed/Trial software system _ Buffering Hotkeys_SQL.au3 ADODB.Connection _ Search 2d Arrays_SplashTextWithGraphicOn() _ Adjust Screen GammaTransparent Controls _ Eventlogs without the crap_GuiCtrlCreateFlash() _ Simple Interscript communication[u]Websites[/u]Curious Campers VW Hightops Lambert Plant Hire 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