Jump to content

Value of 0 = "" ?


Recommended Posts

Maybe I'm crazy

If I store the 0 as a string it works fine but as a number the field is read as empty.

#include <Array.au3>
Dim $array[5]
$Array[0] = 1
$Array[1] = 2
$Array[2] = 3
$Array[3] = 0
$Array[4] = 5
_ArrayDisplay($array)
For $i = 0 To 4
If $array[$i] = "" Then MsgBox(0,$i & " is blank", $array[$i])
Next
Exit

 "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Link to comment
Share on other sites

Try this:

#include <Array.au3>
Dim $array[5]
$Array[0] = 1
$Array[1] = 2
$Array[2] = 3
$Array[3] = 0
$Array[4] = 5
_ArrayDisplay($array)
For $i = 0 To 4
If $array[$i] == "" Then MsgBox(0,$i & " is blank", $array[$i])
Next
Exit

Behavior of the "simple" comparison using one = comparison has always surprised me. Therefore I choose to use the == comparison which is much simpler to understand and to use.

Link to comment
Share on other sites

Interesting never heard of that one. Learn a little more of the increased level of my stupidity everyday lol

I wanted to smash my had against the wall with this lastnight lol

Thanks :P

Edited by ken82m

 "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Link to comment
Share on other sites

yeah definitely a good little tidbit for the book.

I mean unless your a die hard programmer or already killed yoruself on this one who's gonna know.

And from the looks of it, it's the only exception to the quick summary they give so couldn't hurt to add an extra line.

 "I believe that when we leave a place, part of it goes with us and part of us remains... Go anywhere, when it is quiet, and just listen.. After a while, you will hear the echoes of all our conversations, every thought and word we've exchanged.... Long after we are gone our voices will linger in these walls for as long as this place remains."

Link to comment
Share on other sites

I think the more common rule to describe this behavior is that the strings to compare them with are converted into numbers if they only contain numbers.

For example these are all true:

3 = "3"

2 = "2"

1 = "1"

0 = ""

since "" evaluates 0 .. It is an exception because there is nothing in the string. But 0 is nothing too so, it is correct in one way or another.

Link to comment
Share on other sites

In a case like that you can also use String() to get valid results as well. Yes...it's in the help file.

#include <Array.au3>
Dim $array[5]
$Array[0] = 1
$Array[1] = 2
$Array[2] = 3
$Array[3] = 0
$Array[4] = 5
_ArrayDisplay($array)
For $i = 0 To 4
If String($array[$i]) = "" Then MsgBox(0,$i & " is blank", $array[$i])
Next
Exit

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

Interesting never heard of that one. Learn a little more of the increased level of my stupidity everyday lol

I wanted to smash my had against the wall with this lastnight lol

Thanks :P

Before you make bloody forehead prints on the wall, think about where this comes from.

AutoIt is meant to be an easy to use scripting language for people (like me) who are not "real" programmers. So the Devs have spent much effort keeping you from having to worry about one of the central headaches of "real" programming: strict variable typing.

In many (most!) languages, what you did will actually fail to compile or run at all or crash the executable because you compared an integer type to a string type without converting to matching types first. AutoIt tries to keep you from worrying too much about variable types by performing automated type conversions. You are seeing a (documented, if you look for it) side effect of that automatic type conversion in AutoIt.

:unsure:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

I went throught the same grief as the OP a couple months ago trying to get Autoit to distinguish between an ASCII "0" and a NULL.

I still consider the feature undocumented, since I didn't find it when I went looking. I wanted to know how Autoit arithmetic/comparison operators behaved and went to the "Language Reference - Operators" page in the helpfile. The difference between the = and == operators is not described there. I'd think adding just a few words on that page would make the helpfile more accurate, and save a few forum questions a year.

While I'm on the subject of documentation <wink>: The "wildcards" topic under FileFindFirstFile needs a major rewrite. It is way off base!

(PS - I'm not trying to point out flaws as some sort of 'gotcha', I'm detail-oriented, and just wanting Autoit to be the best it can be.)

Link to comment
Share on other sites

I went throught the same grief as the OP a couple months ago trying to get Autoit to distinguish between an ASCII "0" and a NULL.

I still consider the feature undocumented, since I didn't find it when I went looking. I wanted to know how Autoit arithmetic/comparison operators behaved and went to the "Language Reference - Operators" page in the helpfile. The difference between the = and == operators is not described there. I'd think adding just a few words on that page would make the helpfile more accurate, and save a few forum questions a year.

While I'm on the subject of documentation <wink>: The "wildcards" topic under FileFindFirstFile needs a major rewrite. It is way off base!

(PS - I'm not trying to point out flaws as some sort of 'gotcha', I'm detail-oriented, and just wanting Autoit to be the best it can be.)

Pointing out flaws, especially if they come with some thought and a proposed solution (like a proposed new verbiage for the help file entry) are not a problem. In fact, that would be good forum citizenship. You could best do that by creating a new ticket on Bug Trac, identifying the component as "Documentation".

Don't be too hurt if the change is rejected (not that it's inevitable). The Operators section now says:

== Tests if two values are equal (case sensitive if used with strings)

The behavior some users are not expecting is the automatic type conversion that occurs. If you use just '=' and a number is one of the values, then type conversion to a number is done the other value. If you use '==', which is only used for case-sensitive string compares, then it forces conversion of both values to strings for the compare. The confusion is not the operator, which does exactly what it says, but with AutoIt's automatic conversion of values. Perhaps that could use some more documentation.

:P

Edit: Demo of the difference in conversion results:

; Numeric compares with '='
;   Treats each example like:  0 = Number($x)
ConsoleWrite((0 = 0) & @LF); True
ConsoleWrite((0 = "0.0") & @LF); True
ConsoleWrite((0 = "0") & @LF); True
ConsoleWrite((0 = "") & @LF); True 
ConsoleWrite((0 = ".") & @LF); True
ConsoleWrite((0 = "zero") & @LF); True
ConsoleWrite((0 = "") & @LF); True
ConsoleWrite((0 = Chr(0)) & @LF); True

ConsoleWrite(@LF)

; String compares with '=='
;   Treats each example like:  String($x) == String($y)
ConsoleWrite((0 == 0) & @LF); True
ConsoleWrite((0 == "0.0") & @LF); False
ConsoleWrite((0 == "0") & @LF); True
ConsoleWrite((0 == "") & @LF); False 
ConsoleWrite((0 == ".") & @LF); False
ConsoleWrite((0 == "zero") & @LF); False
ConsoleWrite((0 == "") & @LF); False
ConsoleWrite((0 == Chr(0)) & @LF); False

Note the second case in the second group, where 0 == "0.0" is FALSE.

Edit2: Added null examples to the demo.

Edited by PsaltyDS
Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Maybe I'll try a Bug Trac (if it will let me in) and suggest the "Language Reference - Operators" state the following:

= Tests if two values are equal (case insensitive, zero and null treated as equal).

== Tests if two values are equal (case sensitive, zero and null treated as not equal).

That page need not delve into explaining why operators work as they do, but it ought to describe what results to expect when you employ one of them.

Link to comment
Share on other sites

no, there is no === in autoit

You can create a func for this:

Func TYPEEQUAL(ByRef $Var1, ByRef $Var2)
    ; Prog@ndy
    Switch (VarGetType($Var1) == VarGetType($Var2))
        Case True
            Return $Var1==$Var2
        Case Else
            Return False
    EndSwitch
EndFunc

*GERMAN* [note: you are not allowed to remove author / modified info from my UDFs]My UDFs:[_SetImageBinaryToCtrl] [_TaskDialog] [AutoItObject] [Animated GIF (GDI+)] [ClipPut for Image] [FreeImage] [GDI32 UDFs] [GDIPlus Progressbar] [Hotkey-Selector] [Multiline Inputbox] [MySQL without ODBC] [RichEdit UDFs] [SpeechAPI Example] [WinHTTP]UDFs included in AutoIt: FTP_Ex (as FTPEx), _WinAPI_SetLayeredWindowAttributes

Link to comment
Share on other sites

Ok. And for the reference, there is no null either. So stop saying null.

Isn't there both a null character, Chr(0), and a null string (empty)?

$x = Chr(0) & Chr(0); 2-character string
$y = 0
$z = ""
If $x = $y then beep(500,1000)
Sleep(100)
If $x = $z then beep(500,1000)
Sleep(100)
If $y = $z then beep(500,1000)
Sleep(100)
If $x == $y then beep(1000,500)
Sleep(100)
If $x == $z then beep(1000,500)
Sleep(100)
If $y == $z then beep(1000,500)

Edit: All 3 variables test True with =, and all 3 false with ==

Edited by Spiff59
Link to comment
Share on other sites

0 is not a NUL character. NUL is the ascii code 0 not the number 0 which is actually ascii code 48.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

0 is not a NUL character. NUL is the ascii code 0 not the number 0 which is actually ascii code 48.

I didn't say it was, but you agree there is a null character, Chr(0).

And Chr(0) equates to both a Chr(48) and an empty string when using the '=" operator.

All three values equate as True with =, and none of them equate via ==.

The "operators" documentation states the only difference between = and == is case-sensitivity.

That's all my argument is, that the documentation is not true, and that a few added words could clarify things.

Edit: typo

Edited by Spiff59
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...