"\" as allegedly invalid character "in an expression"

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
ari
Posts: 6
Joined: 01 Nov 2014, 16:58

"\" as allegedly invalid character "in an expression"

Post by ari » 02 Oct 2022, 14:15

The following code is a part of a longer script, drive1/2, path1/2, filename1/2 and m and s are all correctly assigned variables,
and in this part I want to also assign the variables fullcurrent and targetpath:

Code: Select all

IF
{
(...)
if ( m = 1 ) ; and thus s = 2
{
	fullcurrent := drive2 . ":" . path2 ."." . filename2 ; ERROR LINE 1
	targetpath := drive2 . ":" . path1 ; 2
}
else ; i.e. m = 2 and thus s = 1
{
	fullcurrent := drive1 . ":\" . path1 ."\" . filename1 ; ERROR LINE 3
	targetpath := drive1 . ":\" . path2 ; 4
}
}
ELSE
(...)
Now the if "m = 1" part should look like the "else" part, but I then invariably get an error message that the character "\" is "not allowed in an expression", for error line 1, so I changed that line, and I got the error message for the next line, so I changed that, too, and now I get the error message for error line (and 4 if I also change that error line 3).

I use AHK 1.1.34, and I simply don't understand why the "\" is not accepted within strings in such a concatenation, and it does not seem that "escaping" it would make sense there. (I know it's different in regex strings, but that's not the case here. Also, I did all the if / else / { / } / etc. correctly.)

User avatar
boiler
Posts: 16772
Joined: 21 Dec 2014, 02:44

Re: "\" as allegedly invalid character "in an expression"

Post by boiler » 02 Oct 2022, 15:18

The problem is the . because you didn't put a space around both sides of it. You didn't use proper expression syntax in trying to concatenate a variable and a string, which is what caused the error.

Run this line as you had it by itself:

Code: Select all

fullcurrent := drive1 . ":\" . path1 ."\" . filename1
...and compare to when you run this corrected line:

Code: Select all

fullcurrent := drive1 . ":\" . path1 . "\" . filename1

ari
Posts: 6
Joined: 01 Nov 2014, 16:58

Re: "\" as allegedly invalid character "in an expression"

Post by ari » 02 Oct 2022, 15:59

Thank you so much, Boiler, you are perfectly right. I had stared for 2 hours onto my script, not becoming aware of the missing spaces. It seems I should use an editor which shows them.

I have a list with my "standard" errors, another one being for example using ":" instead of ";" for comments, or not even outcommenting them, so that a previous code-line beneath an "if" is always reached, and other such really "stupid" things... and then I often simply don't grasp them but many minutes later, so my individual "error check-list" really helps, once the error is entered there.

And it's so time-consuming to check, and re-check, and re-check, variable names for possible typos... ;-)

Without your kind help, I wouldn't have found my error but days later; thank you so much again, Boiler!!!

User avatar
boiler
Posts: 16772
Joined: 21 Dec 2014, 02:44

Re: "\" as allegedly invalid character "in an expression"

Post by boiler » 02 Oct 2022, 18:29

Yes, if you used an editor with AHK syntax highlighting, it would make issues like using : instead of ; for comments super obvious, as is demonstrated below.

Code: Select all

MsgBox, Hello ; this is a properly formatted comment as the text highlighting makes clear
MsgBox, Hello : this is not a comment because it does not use a ";" character

Post Reply

Return to “Ask for Help (v1)”