Use of the Day-of-the-Week Variable

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Alexander2
Posts: 348
Joined: 27 Apr 2019, 17:38

Use of the Day-of-the-Week Variable

Post by Alexander2 » 26 Nov 2022, 13:04

Can anyone please explain why when I ran the following script on Saturday, the message was "Today is not Saturday"?

Code: Select all

if %A_DDDD%<>Saturday
MsgBox Today is not Saturday
else
MsgBox Today is Saturday

User avatar
mikeyww
Posts: 26440
Joined: 09 Sep 2014, 18:38

Re: Use of the Day-of-the-Week Variable

Post by mikeyww » 26 Nov 2022, 14:50

Use expressions.

Code: Select all

If (A_DDDD = "Saturday")
     MsgBox Today is Saturday
Else MsgBox Today is not Saturday
Explained: If

Alexander2
Posts: 348
Joined: 27 Apr 2019, 17:38

Re: Use of the Day-of-the-Week Variable

Post by Alexander2 » 27 Nov 2022, 09:59

mikeyww wrote:
26 Nov 2022, 14:50
Use expressions.

Code: Select all

If (A_DDDD = "Saturday")
     MsgBox Today is Saturday
Else MsgBox Today is not Saturday
Explained: If
Thank you. Removing the percent signs has solved the problem. Apparently Autohotkey treats variables in the If expressions differently when these variables are enclosed by percent signs.

User avatar
mikeyww
Posts: 26440
Joined: 09 Sep 2014, 18:38

Re: Use of the Day-of-the-Week Variable

Post by mikeyww » 27 Nov 2022, 10:06

You are correct.

Code: Select all

a = r
r = 5
MsgBox,, Expression a    , % a
MsgBox,, Expression `%a`%, % %a%
Explained: Expression operators
If a variable is enclosed in percent signs within an expression (e.g. %Var%), whatever that variable contains is assumed to be the name or partial name of another variable (if there is no such variable, %Var% resolves to a blank string).

Alexander2
Posts: 348
Joined: 27 Apr 2019, 17:38

Re: Use of the Day-of-the-Week Variable

Post by Alexander2 » 27 Nov 2022, 10:12

I needed help with the sample script because I have a script which should check whether it is not Wednesday or Saturday when it is run. Can you also explain why the following expression does not work as intended?

Code: Select all

if (A_DDDD<>"Wednesday" or "Saturday")

User avatar
mikeyww
Posts: 26440
Joined: 09 Sep 2014, 18:38

Re: Use of the Day-of-the-Week Variable

Post by mikeyww » 27 Nov 2022, 10:25

You are using or as you would use it in English, but or is actually a logical operator that is used on one pair of expressions at a time. Each pair of expressions must be intact, though there is an order of precedence that is also used (explained in documentation).

Code: Select all

If ((A_DDDD != "Wednesday") AND (A_DDDD != "Saturday"))
 MsgBox, Not!
The middle parentheses are not necessary here, but they illustrate the point that AND and OR operate upon one pair of expressions. This type of operator is called a binary operator, because it has exactly two operands. = and != are also binary operators. Thus, the script above shows three operations: two !=, followed by one AND that uses the results of the first two.

Explained: OR

Code: Select all

expression1 := A_DDDD != "Wednesday"
expression2 := A_DDDD != "Saturday"
If (expression1 AND expression2)
 MsgBox, Not!

Code: Select all

expression1 := A_DDDD != "Wednesday"
expression2 := "Saturday"            ; Always true (in "If") because this string is neither null nor zero
If expression2
 MsgBox, Expression2 is true
If (expression1 OR expression2)      ; Always true if one expression is always true
 MsgBox, At least one expression is true

Alexander2
Posts: 348
Joined: 27 Apr 2019, 17:38

Re: Use of the Day-of-the-Week Variable

Post by Alexander2 » 28 Nov 2022, 13:21

mikeyww wrote:
27 Nov 2022, 10:25
You are using or as you would use it in English, but or is actually a logical operator that is used on one pair of expressions at a time. Each pair of expressions must be intact, though there is an order of precedence that is also used (explained in documentation).

Code: Select all

If ((A_DDDD != "Wednesday") AND (A_DDDD != "Saturday"))
 MsgBox, Not!
The middle parentheses are not necessary here, but they illustrate the point that AND and OR operate upon one pair of expressions. This type of operator is called a binary operator, because it has exactly two operands. = and != are also binary operators. Thus, the script above shows three operations: two !=, followed by one AND that uses the results of the first two.

Explained: OR

Code: Select all

expression1 := A_DDDD != "Wednesday"
expression2 := A_DDDD != "Saturday"
If (expression1 AND expression2)
 MsgBox, Not!

Code: Select all

expression1 := A_DDDD != "Wednesday"
expression2 := "Saturday"            ; Always true (in "If") because this string is neither null nor zero
If expression2
 MsgBox, Expression2 is true
If (expression1 OR expression2)      ; Always true if one expression is always true
 MsgBox, At least one expression is true
Thank you very much for the detailed explanations and useful information. I understand now that such operators as AND and OR operate on expressions rather than directly on variables. The variables expression1 and expression2 in your scripts are treated as expressions rather than as variables because they are defined as expressions.

User avatar
mikeyww
Posts: 26440
Joined: 09 Sep 2014, 18:38

Re: Use of the Day-of-the-Week Variable

Post by mikeyww » 28 Nov 2022, 13:35

I wouldn't say that is exactly true. The variables are used as expressions in the specific operations or lines that refer to them. As you have said, treated as expressions.

Yes, operators act upon expressions. These may be variable (names) or other types of expressions.

Rohwedder
Posts: 7512
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: Use of the Day-of-the-Week Variable

Post by Rohwedder » 29 Nov 2022, 06:40

Hallo,
instead of:

Code: Select all

If ((A_DDDD != "Wednesday") AND (A_DDDD != "Saturday"))
	MsgBox, Not!
would be possible:

Code: Select all

If !Instr("Wednesday" "Saturday", A_DDDD)
	MsgBox, Not!

User avatar
mikeyww
Posts: 26440
Joined: 09 Sep 2014, 18:38

Re: Use of the Day-of-the-Week Variable

Post by mikeyww » 29 Nov 2022, 06:49

Code: Select all

If A_WDay in 4,7
     MsgBox, Day!
Else MsgBox, Not!

Alexander2
Posts: 348
Joined: 27 Apr 2019, 17:38

Re: Use of the Day-of-the-Week Variable

Post by Alexander2 » 30 Nov 2022, 12:17

Yes, experienced users like you know a number of other commands which can achieve the same result. The commands which require more knowledge of programming often occupy less space.

User avatar
mikeyww
Posts: 26440
Joined: 09 Sep 2014, 18:38

Re: Use of the Day-of-the-Week Variable

Post by mikeyww » 30 Nov 2022, 12:29

You now have your pick! Different strokes for different folks. Was always true and still is!

Post Reply

Return to “Ask for Help (v1)”