Problem with assigning variable with value ending with colon!!!!

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
kalibrerickson
Posts: 1
Joined: 10 May 2019, 03:37

Problem with assigning variable with value ending with colon!!!!

Post by kalibrerickson » 10 May 2019, 03:43

Not sure if this is a bug or not. But looks like a bug to me...

Check the below code

Code: Select all

Driveget, dlist, list
Loop, Parse, dlist
{
thisDrive=%A_LoopField%:   		; <- this doesn't work, thisDrive is blank here

thisDrive = %A_LoopField%:  	; <- works fine
thisDrive := A_LoopField ":"  	; <- works fine
}

str = abcdef

test_var=%str%
Last edited by kalibrerickson on 10 May 2019, 14:34, edited 1 time in total.

gregster
Posts: 9001
Joined: 30 Sep 2013, 06:48

Re: Problem with assigning variable with value ending with colon!!!!

Post by gregster » 10 May 2019, 12:08

Not a bug, but a pitfall of the "legacy" syntax, in which you will have to escape certain characters (like a literal "%"), if they aren't unambigious:

First of all, variable assignment with = ("legacy method") instead of := ("expression method") is deprecated and ideally shouldn't be used anymore (it's still there for backwards compatibility, but will be discontinued in AHK v2).

If you use it, you should use spaces around the = (at least one), otherwise it could happen, in combination with a trailing :, that it is seen as a label name, because the naming conventions for labels are rather generous:
https://www.autohotkey.com/docs/misc/Labels.htm#syntax-and-usage wrote:LabelName:
To create a label, write the label name followed by a colon as shown above. Aside from whitespace and comments, no other code can be written on the same line.

Names: Label names are not case sensitive, and may consist of any characters other than space, tab, comma and the escape character (`). However, due to style conventions, it is generally better to use only letters, numbers, and the underscore character (for example: MyListView, Menu_File_Open, and outer_loop).[...]
As demonstrated here:

Code: Select all

gosub hello=world
hello =world:	; this assigns literal text, like also hello = world:
msgbox % hello
Exitapp

hello=world:	; this a label name
msgbox label was called
return
Or, you just escape the trailing : with AHK's escape character ` to make clear that this isn't a label name, but a variable assignment.

Code: Select all

hello=world`:		; variable assignment
msgbox % hello
Exitapp
(The syntax highlighter of the codebox isn't smart enough to see that this isn't a label. Btw, you seemed to have difficulties in using the codebox. Just put [code][/code]-tags around your code; there is also a button for it, in the editor: fifth from the left. I fixed it for you in your post above.)

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Problem with assigning variable with value ending with colon!!!!

Post by swagfag » 10 May 2019, 12:36

gregster wrote:
10 May 2019, 12:08
...in combination with a trailing :, that it is seen as a label name...
:facepalm:
i was looking at this wondering what could possibly be wrong with it, cuz i didnt have the means to test it at the time
horrendous

gregster
Posts: 9001
Joined: 30 Sep 2013, 06:48

Re: Problem with assigning variable with value ending with colon!!!!

Post by gregster » 10 May 2019, 12:42

Yeah, it's really not nice. I had an early suspicion, but had to confirm.
Luckily, people rarely use label names with =s in it - probably because they don't expect it to work :D

Edit: Of course, a literal label name like thisDrive=%A_LoopField% or worse would even be more horrendous, but it's possible to use it (if you escape the %s while calling it :crazy: - it's a way of code obfuscation ;) )

:think:

Code: Select all

gosub `"thisDrive=`%A_LoopField`%`"....`%§![\} 	
Exitapp

"thisDrive=%A_LoopField%"....%§![\}: 	; this a label name
msgbox label was called
return

sapianvid
Posts: 2
Joined: 08 Dec 2021, 04:43

Re: Problem with assigning variable with value ending with colon!!!!

Post by sapianvid » 08 Dec 2021, 11:33

actually i also same issue while assigning value

gregster
Posts: 9001
Joined: 30 Sep 2013, 06:48

Re: Problem with assigning variable with value ending with colon!!!!

Post by gregster » 10 Dec 2021, 04:28

sapianvid wrote:
08 Dec 2021, 11:33
actually i also same issue while assigning value
If you still have problems after reading the above, please ask a specific question.
You could also show the code you are having problems with.

sapianvid
Posts: 2
Joined: 08 Dec 2021, 04:43

Re: Problem with assigning variable with value ending with colon!!!!

Post by sapianvid » 15 Dec 2021, 04:08

Thanks for your quick reply.

Post Reply

Return to “Ask for Help (v1)”