Sign in to follow this  
Followers 0
ripdad

1 and string = bug?

28 posts in this topic

Can someone confirm a bug?

; test digit and string if equal to 1
;==========================================
$s = '1 String'
If $s = 1 Then
    MsgBox(0,'1',$s)
EndIf

; test with front-end space
;==========================================
$s = '      1 String'
If $s = 1 Then
    MsgBox(0,'2',$s)
EndIf

; test multi-line with space
;==========================================
$s = @CRLF & @CRLF & '      1 String'
If $s = 1 Then
    MsgBox(0,'3',$s)
EndIf

; swap digit and string
;==========================================
$s = 'String 1'
If $s = 1 Then
    MsgBox(0,'4',$s)
EndIf

Make yourself sheep and the wolves will eat you - Benjamin Franklin

Share this post


Link to post
Share on other sites



You're not comparing an int to a string, you are putting 1 in the string.

Share this post


Link to post
Share on other sites

If you compare a number with a string the string will be converted to a number. You can check the result of the conversion with

msgBox(0, "", Number($s))

All strings starting with a number (examples 1 to 3) are converted to 1. $s = 'String 1' converts to 0 and therefore is not equal 1.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

You're not comparing an int to a string, you are putting 1 in the string.

Wrong. After "If" always a comparison is being done.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Just look at his "if" statement. He's using a single "=" sign instead of two. what does "$string = 1" mean in a if statement?

Share this post


Link to post
Share on other sites

From the help file (Language Reference > DataTypes)

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

You're doing a boolean comparison by using the 1 as a number, so an non-empty string will equal 1 because True = 1.

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
Here you find all available operators.

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Just look at his "if" statement. He's using a single "=" sign instead of two. what does "$string = 1" mean in a if statement?

In AutoIt the double "=" is used as a case sensitive string comparison, a single quote is used, in this example, as a comparison without case sensitvitiy.

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

To sum it up: No, it's not a bug. AutoIt works as designed.


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

I'm really sorry then. That's what I get for thinking in C# in Autoit forums ;). Sorry again.

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

From the help file (Language Reference > DataTypes)

You're doing a boolean comparison by using the 1 as a number, so an non-empty string will equal 1 because True = 1.

I just tested this and it doesn't check out

$s = '2 some obfuscated string'
If $s = 1 Then
    MsgBox(0,'1',$s) ; fails
EndIf

Notice this:

$s = '2 some obfuscated string'
If $s = 2 Then
    MsgBox(0,'1','first') ; works
EndIf
$s = '1 some obfuscated string'
If $s = 2 Then
    MsgBox(0,'2','second') ; fails
EndIf

I thought it was thinking 1 = true also until i tested this

I think what's happening is that since it's comparing the string to a number, it's converting the string to a number... which all alpha characters are scrapped from the string.

Disregard below... I was on something at the time I wrote it

You can even put the numbers at the end of the string and they will return true.

$s = 'some obfuscated string 2'
If $s = 2 Then
    MsgBox(0,'1','1') ; works
EndIf
$s = 'some obfuscated string 2'
If $s = 1 Then
    MsgBox(0,'2','2') ; fails
EndIf

Edited by mechaflash213

Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Share this post


Link to post
Share on other sites

I Expect StringInStr($s, 1) to return 1

but not an If equal to statement.

Something new learned today, I guess.

Thanks for your time and help.

Now I'm off to write a function to deal with it....


Make yourself sheep and the wolves will eat you - Benjamin Franklin

Share this post


Link to post
Share on other sites

I just tested this and it doesn't check out

I think what's happening is that since it's comparing the string to a number, it's converting the string to a number... which all alpha characters are scrapped from the string. You can even put the numbers at the end of the string and they will return true.

Yes, you're right, when doing a comparison against a number, it's converting the string to a number, got that part wrong. ;)

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

I just tested this and it doesn't check out

$s = '2 some obfuscated string'
If $s = 1 Then
    MsgBox(0,'1',$s) ; fails
EndIf

Notice this:

$s = '2 some obfuscated string'
If $s = 2 Then
    MsgBox(0,'1','first') ; works
EndIf
$s = '1 some obfuscated string'
If $s = 2 Then
    MsgBox(0,'2','second') ; fails
EndIf

I thought it was thinking 1 = true also until i tested this

I think what's happening is that since it's comparing the string to a number, it's converting the string to a number... which all alpha characters are scrapped from the string. You can even put the numbers at the end of the string and they will return true.

$s = 'some obfuscated string 2'
If $s = 2 Then
    MsgBox(0,'1','1') ; works
EndIf
$s = 'some obfuscated string 2'
If $s = 1 Then
    MsgBox(0,'2','2') ; fails
EndIf

Autoit does it's best to figure out what a user is trying to do, when code is bad.

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

Monkey's are, like, natures humans.

Share this post


Link to post
Share on other sites

Autoit does it's best to figure out what a user is trying to do, when code is bad.

;) I feel edumacated


Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Share this post


Link to post
Share on other sites

#17 ·  Posted (edited)

I Expect StringInStr($s, 1) to return 1

but not an If equal to statement.

Something new learned today, I guess.

Thanks for your time and help.

Now I'm off to write a function to deal with it....

Where do you find a StringInStr in the OPs code? If you pass a parameter as number to the function but a string is expected it is translated to a string.

$s = '    1 String'
ConsoleWrite(Stringinstr($s, 1) & @LF)
returns 7 and is equivalent to
$s = '    1 String'
ConsoleWrite(Stringinstr($s, "1") & @LF)
Edited by water

My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-04-18 - Version 1.4.8.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-27 - Version 1.3.1.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
PowerPoint (2015-06-06 - Version 0.0.5.0) - Download - General Help & Support

Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

#18 ·  Posted (edited)

I Expect StringInStr($s, 1) to return 1

but not an If equal to statement.

Something new learned today, I guess.

Thanks for your time and help.

Now I'm off to write a function to deal with it....

StringInStr() returns the position of the value you're searching for. If 1 is at the first position, it will return 1. If there are 6 spaces before the 1, it will return 7 etc. If you're just trying to make sure that it's there, just do a

If StringInStr($s, 1) Then msgbox(0,"","It's there")

Which the function returns True (non-zero), else it returns False (zero)

Edited by mechaflash213

Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

Share this post


Link to post
Share on other sites

Okay, I get it.

That string could be an either-or combination. Thats my point.

I will still have to write a function for it.


Make yourself sheep and the wolves will eat you - Benjamin Franklin

Share this post


Link to post
Share on other sites

Okay, I get it.

That string could be an either-or combination. Thats my point.

I will still have to write a function for it.

Why not just make the number a string? Or check if what you're comparing to is an integer first, then do the comparison?

$s = '1 some obfuscated string'
If $s = '1' Then
    MsgBox(0,'1',$s) ; fails
EndIf

;;;;;;;;; or

$s = '1 some obfuscated string'
If IsInt($s) And $s = 1 Then
    MsgBox(0,'1',$s) ; fails
EndIf

Spoiler

“Hello, ladies, look at your man, now back to me, now back at your man, now back to me. Sadly, he isn’t me, but if he stopped using ladies scented body wash and switched to Old Spice, he could smell like he’s me. Look down, back up, where are you? You’re on a boat with the man your man could smell like. What’s in your hand, back at me. I have it, it’s an oyster with two tickets to that thing you love. Look again, the tickets are now diamonds. Anything is possible when your man smells like Old Spice and not a lady. I’m on a horse.”

 

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