ScriptKid
ScriptKid

Reputation: 13

Autohotkey If Statement makes only one result

I wanted to run a program on working day only, so I wrote a AutoHotKey script to check "today is sunday or holiday"

But my script doesn't work and I didn't get what the problem is. Here is the code.

Result = "WorkingDay"

if(A_Wday = 1) {
Result = "Sunday"
}

HolidayList = 0101|0211|0212|0213|0301|0409|0501|0505|0519|0606|0815|0920|0921|0922|1003|1009|1225
StringSplit,HoliDay,HoliDayList,|
StringLeft,Today,A_Now,8
StringRight,Today,Today,4

Loop %Holiday0%
    {
    CheckDay := HoliDay%A_index%
    ; msgbox, %Today% = %CheckDay%
    if ( %Today% == %CheckDay% ) {
    Result = "Holiday"
    break
    }
}

msgbox, %Today% = %Result%

The problem is that the "Result" variable return only "Holiday"

Please help me out......... Thanks in advance.

Upvotes: 0

Views: 281

Answers (1)

0x464e
0x464e

Reputation: 6489

Basically you're just using a whole bunch of legacy code, and trying to mix in some modern non-legacy stuff, and they really don't mix well unless you know very well how to do it.

Biggest issue is this right here:
if ( %Today% == %CheckDay% )
By trying to use the legacy way of referring to variables in a non legacy if ()(docs), bad things happen. You're trying to use dynamic variables, and you really don't want that, so what actually happens is that you check if nothing == nothing, and that's always going to return true.
How you're actually supposed to refer to variables in a modern expression statement, is just
if (Today = CheckDay)
(when comparing, = and == are for case insensitive and case sensitive comparing, you probably meant to do =)

And now it should work.
Here's the whole code in modern AHK:

Result := "WorkingDay"

if (A_Wday = 1) 
    Result := "Sunday"

HolidayList := "0101|0211|0212|0213|0301|0409|0501|0505|0519|0606|0815|0920|0921|0922|1003|1009|1225"
Holidays := StrSplit(HolidayList, "|")
Today := SubStr(A_Now, 5, 4)

for each, Holiday in Holidays
{
    if (Today = Holiday)
    {
        Result := "Holiday"
        break
    }
}

MsgBox, % Today " = " Result

I don't have time to explain it more right now, but to learn about legacy vs modern AHK, I recommend this documentation page and this previous answer of mine:
https://www.autohotkey.com/docs/Language.htm

When to use % or %variable% in AutoHotKey?

Upvotes: 1

Related Questions