Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate

[AHK newbie "tip"]Assignment and String Literal Co


  • Please log in to reply
5 replies to this topic
ChocoBean
  • Guests
  • Last active:
  • Joined: --
not a question, but a summary of 3 hours of RTFM, searching for a way to do a simple task: compare a parameter to a string. It was extremely frustrating because I was under the assumption that the := operator is the same as the = operator for assignment. IT IS NOT. foo=%1% is NOT the same thing as foo:=%1%

What made it worse was the right operand: do i put it in quotes, or not? The answer is: it depends. Below is the comparative result
; %1% is coming in as "foobar" without quotes
foo1=%1% ; using the = operator
; CORRECT : EITHER the variable is quoted with %%s, OR the string literal is in ""s, BUT NOT BOTH
If (foo1 = "FooBar")	
	MsgBox 1 OKAY
IF (foo1 == "foobar")	
	MsgBox 2 OKAY
If (%foo1% = FooBar)		
	MsgBox 3 OKAY
If (%foo1% == foobar)		
	MsgBox 4 OKAY
; INCORRECT
If (foo1 = FooBar)		
	MsgBox 5 InvisibleBox!
IF (foo1 == foobar)		
	MsgBox 6 InvisibleBox!
If (%foo1% = "FooBar")		
	MsgBox 7 InvisibleBox!
If (%foo1% == "foobar")	
	MsgBox 8 InvisibleBox!

foo2:=%1% 
; using the := operator
; INCORRECT
If (foo2 = "FooBar")	
	MsgBox 1b InvisibleBox!
IF (foo2 == "foobar")	
	MsgBox 2b InvisibleBox!
; CORRECT : the string literal CANNOT be in ""s, but the variable can be with or without %%s
If (%foo2% = FooBar)		
	MsgBox 3b OKAY
If (%foo2% == foobar)		
	MsgBox 4b OKAY
If (foo2 = FooBar)		
	MsgBox 5b OKAY
IF (foo2 == foobar)		
	MsgBox 6b OKAY
; INCORRECT
If (%foo2% = "FooBar")		
	MsgBox 7b InvisibleBox!
If (%foo2% == "foobar")	
	MsgBox 8b InvisibleBox!

=( hope some other souls can save some minutes of their lives

engunneer
  • Moderators
  • 9162 posts
  • Last active: Sep 12 2014 10:36 PM
  • Joined: 30 Aug 2005
Sorry you had to spend so much time on it. The relevant documentation pages that discuss exactly what to do are:
<!-- m -->http://www.autohotke...FAQ.htm#percent<!-- m --> , which shows when to use %%
<!-- m -->http://www.autohotke... ... xpressions<!-- m --> , which helps define expressions regarding the last item of the FAQ listing
<!-- m -->http://www.autohotke... ... ession.htm<!-- m --> , which shows that an If Statement that has parentheses around it is automatically an expression, so the same %% rules apply.

Keep in mind that there is a difference between = and == inside an If (expression)

You also have a few mistakes (Things you marked OK that are not doing what you expect). I have added some comments to your code:
; %1% is coming in as "foobar" without quotes
foo1=%1% ; using the = operator
; CORRECT : EITHER the variable is quoted with %%s, OR the string literal is in ""s, BUT NOT BOTH


;compares the contents of the variable foo1 with the string FooBar
If (foo1 = "FooBar")   
   MsgBox 1 OKAY

;same, but using case sensitive compare
IF (foo1 == "foobar")   
   MsgBox 2 OKAY

;NOT OKAY, compares the contents of the variable who's name is contained in foo1 with the contents of the Variable FooBar. You list this as OK because "" = ""
;technically evaluates to If (foobar = foobar) which is true
If (%foo1% = FooBar)    
   MsgBox 3 OKAY

;same as above, NOT OKAY, or at least is not true for the right reason.
If (%foo1% == foobar)      
   MsgBox 4 OKAY

;compares the contents of the variable foo1 (which is "foobar") to the contents of the variable foobar, (which is blank)
If (foo1 = FooBar)      
   MsgBox 5 InvisibleBox!

;same as above, but case sensitive
IF (foo1 == foobar)      
   MsgBox 6 InvisibleBox!

;NOT OKAY, technically evaluates to If (foobar = "FooBar") which is false because the variable foobar currently contains ""
If (%foo1% = "FooBar")      
   MsgBox 7 InvisibleBox!

;NOT OKAY, technically evaluates to If (foobar == "foobar") which is false because the variable foobar currently contains ""
If (%foo1% == "foobar")   
   MsgBox 8 InvisibleBox!

foo2:=%1%
; using the := operator

;technically, this sets foo2 equal to the value of the variable who's name is contained in %1%
; in other words, foo2 = %foobar%
;since foobar is "", foo2 is now also ""

; checks if the value of foo2 (which is "") is equal to the string "FooBar"
If (foo2 = "FooBar")   
   MsgBox 1b InvisibleBox!

; checks if the value of foo2 (which is "") is equal to the string "foobar" (Case sensitive)
IF (foo2 == "foobar")   
   MsgBox 2b InvisibleBox!

;checks if the value of the variable who's name is contained in foo2 (variable = "", so value also = "") matches the value contained in FooBar, which is also ""
If (%foo2% = FooBar)      
   MsgBox 3b OKAY

;same as above, but case sensitive
If (%foo2% == foobar)      
   MsgBox 4b OKAY

; checks if the value of foo2 (which is "") is equal to the value contained in the variable FooBar (which is "")
If (foo2 = FooBar)      
   MsgBox 5b OKAY

;same as above, but case sensitive
IF (foo2 == foobar)      
   MsgBox 6b OKAY

;checks if the value of the variable who's name is contained in foo2 (variable = "", so value also = "") matches the string "FooBar"
If (%foo2% = "FooBar")      
   MsgBox 7b InvisibleBox!

;checks if the value of the variable who's name is contained in foo2 (variable = "", so value also = "") matches the string "foobar" (case sensitive)
If (%foo2% == "foobar")   
   MsgBox 8b InvisibleBox!

so the whole second section should explain why the results you got are the way they are.

In short:
Inside an expression, all variables do not have %% unless you want to double dereference the variable. All strings have "" around them.

Chocobean
  • Guests
  • Last active:
  • Joined: --
Thanks very much engunneer!
the FAQ helps explain things quite a bit, and it's all coming together now.... =)

eawedat
  • Members
  • 61 posts
  • Last active: Aug 31 2011 07:55 PM
  • Joined: 27 Aug 2011
Is there a built-in function to compare two strings rather than using the equal sign ( = , ==) ?

  • Guests
  • Last active:
  • Joined: --
IsEqual(str1, str2) {

    return str1 == str2

}


eawedat
  • Members
  • 61 posts
  • Last active: Aug 31 2011 07:55 PM
  • Joined: 27 Aug 2011
str1=abc
str2=abc
IsEqual(str1,str2) {
    msgbox Strings are equal!!
}

try it , not working for me.
I tried also IfEqual...