If statement not working

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
JensHaglof
Posts: 81
Joined: 04 Jul 2017, 11:41

If statement not working

Post by JensHaglof » 30 Nov 2022, 09:19

Code that assigns a value to the variable PackbordsTyp:

Code: Select all

Path2:= "\Msg.box"
FileRead, CompList, G:\Data\CompList.ants
CompListRows := StrSplit(CompList, "`n")
Loop {
	CompListRow := StrSplit(CompListRows[A_Index], "`t")
	If (CompListRow[2] = A_ComputerName) {
		Packbord:=CompListRow[1]
		PackbordsTyp:=CompListRow[3]
		Break
	}
}
Further down in the code this happens:

Code: Select all

FileRead, MsgFile, %a_scriptdir%%Path2%
MessageFile := StrSplit(MsgFile, "`n")
MessageFilePackBord := MessageFile[1]

msgbox %PackbordsTyp%
msgbox %MessageFilePackBord%

If (MessageFilePackBord = PackbordsTyp) {
	msgbox success
}
The If statement in the end doesn't work. The MsgBoxes both shows the exact same value for PackbordsTyp and MessageFilePackBord, still it doesn't enter the If statement. I'm scratching my head here. I've tried:

Code: Select all

If (MessageFilePackBord = PackbordsTyp) {
If MessageFilePackBord = PackbordsTyp {
If (MessageFilePackBord == PackbordsTyp) {
If (%MessageFilePackBord% = %PackbordsTyp%) {
If (%MessageFilePackBord% == %PackbordsTyp%) {
If (Trim(MessageFilePackBord, " ") = Trim(PackbordsTyp, " "))
None of them worked. Anyone have a clue why?

sofista
Posts: 644
Joined: 24 Feb 2020, 13:59
Location: Buenos Aires

Re: If statement not working

Post by sofista » 30 Nov 2022, 09:31

Perhaps it is related, so worth a try: Something similar happened to me a few days ago, and the problem was only solved when the script was saved as UTF-8-BOM.

RussF
Posts: 1229
Joined: 05 Aug 2021, 06:36

Re: If statement not working

Post by RussF » 30 Nov 2022, 09:32

Try this:

Code: Select all

msgbox XX%PackbordsTyp%XX
msgbox XX%MessageFilePackBord%XX
and see if there is a space character at the beginning or end of one of your variables. That would cause an inequality. You could then use Trim() to fix it.

Russ

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

Re: If statement not working

Post by Rohwedder » 30 Nov 2022, 09:36

Hallo,
try:

Code: Select all

If Trim(MessageFilePackBord," `t`r") = Trim(PackbordsTyp," `t`r") {
	msgbox success
}

JensHaglof
Posts: 81
Joined: 04 Jul 2017, 11:41

Re: If statement not working

Post by JensHaglof » 30 Nov 2022, 09:47

RussF wrote:
30 Nov 2022, 09:32
Try this:

Code: Select all

msgbox XX%PackbordsTyp%XX
msgbox XX%MessageFilePackBord%XX
and see if there is a space character at the beginning or end of one of your variables. That would cause an inequality. You could then use Trim() to fix it.

Russ
I tried your suggestion and lo and behold MessageFilePackBord had a Carriage return at the end. I tried

Code: Select all

MessageFilePackBord := Trim(MessageFile[1], "`n")
but it didn't work.

Code: Select all

MessageFilePackBord := SubStr(MessageFile[1], 1,-1)
did work though.

Thanks for the suggestion!

just me
Posts: 9406
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: If statement not working

Post by just me » 30 Nov 2022, 11:12

If you want to split a file read from disk into a line array you almost always need

Code: Select all

StrSplit(FileContent, "`r`n")
If you're unsure, you can use

Code: Select all

StrSplit(FileContent, "`n", "`r")

JensHaglof
Posts: 81
Joined: 04 Jul 2017, 11:41

Re: If statement not working

Post by JensHaglof » 01 Dec 2022, 02:50

just me wrote:
30 Nov 2022, 11:12
If you want to split a file read from disk into a line array you almost always need

Code: Select all

StrSplit(FileContent, "`r`n")
If you're unsure, you can use

Code: Select all

StrSplit(FileContent, "`n", "`r")
Thank you! That solved it!

Post Reply

Return to “Ask for Help (v1)”