Methods using commandline arguments

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
MrSlimbrowser
Posts: 2
Joined: 23 May 2016, 00:10

Methods using commandline arguments

23 May 2016, 00:28

Good Morning,
this is more a question than a bug report.
I tried to launch a script with arguments and pass the arguments to a method. Following example:

Code: Select all

x := employee(1)
MsgBox, %x%
return

employee(staff_number)
{
	return staff_number
}
Since 'x := employee(1)' is a Methodcall I should have to use the variable itself without %. Instead of the variables content the Method receives the plain number '1' so the MsgBox says '1'.

Other variables work like a charm, following example also works

Code: Select all

a = %1%
x := employee(a)
MsgBox, %x%
return

employee(staff_number)
{
	return staff_number
}
In other words: I just can't use ' x := 1 ' to assign the content of the variable 1 to x. I also tried ' x := A_AhkVersion ' which works, so I think it should also work for arguments passed to the script.

I hope my english was understandable, it's not my first language, sorry ;)
greetings
-, Slim
just me
Posts: 9449
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Methods using commandline arguments

23 May 2016, 02:43

MrSlimbrowser wrote:this is more a question than a bug report.
So why didn't you post it in "Ask For Help"?
User avatar
sinkfaze
Posts: 616
Joined: 01 Oct 2013, 08:01

Re: Methods using commandline arguments

23 May 2016, 07:26

This has been moved to 'Ask for Help'.
just me
Posts: 9449
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Methods using commandline arguments

23 May 2016, 08:06

Code: Select all

x := employee(1)
is actually a function call. And parameters passed to functions must be expressions. That's also true for method calls.

Within expressions pure numeric values are always treated as numbers and cannot be used as a variable's name. Therefore you have to store either the value or the name of the argument variable 1 in a variable with a non-numeric name to pass it to the function:

Code: Select all

; Either the value ...
Arg = %1% ; command syntax
x := employee(Arg)
; ... or the name
Arg := 1
x := employee(%Arg%) ; double-deref
MrSlimbrowser
Posts: 2
Joined: 23 May 2016, 00:10

Re: Methods using commandline arguments

23 May 2016, 13:09

Thanks to you too for help and moving the thread :)
just me wrote:
MrSlimbrowser wrote:this is more a question than a bug report.
So why didn't you post it in "Ask For Help"?
Good question ....

Seeing your explanation I searched one more and found the regading text in the docs:
"Although a variable name may consist entirely of digits, this is generally used only for incoming command line parameters. Such numeric names cannot be used in expressions because they would be seen as numbers rather than variables."
(source: https://autohotkey.com/docs/Variables.htm#Intro)

Only one more question.. why would one virtually declare that a variablename should never be a pure number and then use one as a predefined one, marking it as an exception?
just me
Posts: 9449
Joined: 02 Oct 2013, 08:51
Location: Germany

Re: Methods using commandline arguments

23 May 2016, 15:37

MrSlimbrowser wrote:...why would one virtually declare that a variablename should never be a pure number and then use one as a predefined one...
MrSlimbrowser wrote:... Such numeric names cannot be used in expressions ...
User avatar
trismarck
Posts: 506
Joined: 30 Sep 2013, 01:48
Location: Poland

Re: Methods using commandline arguments

23 May 2016, 19:11

MrSlimbrowser wrote:...why would one virtually declare that a variablename should never be a pure number and then use one as a predefined one...
Yes, to clarify, I guess it's because there was a time in the past when Autohotkey lacked expression mode altogether.
MrSlimbrowser wrote:In other words: I just can't use ' x := 1 ' to assign the content of the variable 1 to x.
Within an expression, it is actually possible to force 1 to be treated as a variable (and not as a number) by using a double [de]reference.

Code: Select all

x := 1%empty%
(Double reference)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: mikeyww and 217 guests