A_args and subroutines Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
joefiesta
Posts: 502
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

A_args and subroutines

27 Feb 2018, 11:00

I am trying out use of the new built-in variable A_Args.

At first I spent lots of time trying to figure out why it doesn't show the parameters for a function. (A script has parameters. A function has parameters. A_args are the parameters. hmmm.... I think a doc note is in order about what for many may be a misunderstanding of A_args.)

Anyway, I can't figure this out this script. Why does the MSGBOX display show null values for A_args[1], A_args[2] etc.?



Test.ahk:

Code: Select all

st := "now  is    the   time for all good men ..."
msgbox % subword(st,3,2)
exitapp
subword.ahk:

Code: Select all

SubWord(Phrase,Num,Length=1,Delimiter=" ") {
global
msgbox % a_args[1]  a_args[2]  a_args[3]
for n, param in A_Args  ; For each parameter:
   {
       MsgBox Parameter number %n% is %param%.
   }
}

One confusing thing is: when I open TEST.ahk and look at the variables, A_args is shown.
It looks like the global statement doesn't access A_args.
Please don't tell me A_args is not a variable. If you do, then: why is it listed as a variable when I open TEST.ahk and view it's variable?
gregster
Posts: 9113
Joined: 30 Sep 2013, 06:48

Re: A_args and subroutines

27 Feb 2018, 11:27

As far as I understand it, it just lists command line parameters and not function parameters. That means, it lists the parameters that you use when you run a script via command line, like this:run AutoHotkey.exe somescript.ahk blue green C:\test\yxz.txt If you don't use command line parameters, the variable A_args exists, but just stays blank.

Run this in one script

Code: Select all

run autohotkey.exe somescript.ahk blue green C:\test
with another script: somescript.ahk

Code: Select all

msgbox % a_args[1] " "  a_args[2]  " " a_args[3]		; returns blue green C:\test
msgbox %1% %2% %3%     ; old way - equivalent
There was another way already to do that (see above), but I think there was a reason behind it that they added the new variable, that i don't remember at the moment :D
Changelog just states: Added A_Args as an alternative to the numbered variables

Related: https://autohotkey.com/docs/Scripts.htm#cmd, see 'Script Parameters' section
https://autohotkey.com/docs/Variables.htm#Args:
A_Args
[v1.1.27+] Read/write: Contains an array of command line parameters. For details, see Passing Command Line Parameters to a Script.
Last edited by gregster on 27 Feb 2018, 11:49, edited 1 time in total.
gregster
Posts: 9113
Joined: 30 Sep 2013, 06:48

Re: A_args and subroutines

27 Feb 2018, 11:43

I think A_Args was first introduced in AHK v2 to replace the pseudo-array %1%, %2% etc. with an array (https://autohotkey.com/v2/v2-changes.htm).
Back in the day, when the old way was introduced, AHK didn't support real arrays. In v1.1.27+ they kept the pseudo-array for compatibility, when they added A_args as an array alternative.
joefiesta
Posts: 502
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Re: A_args and subroutines

27 Feb 2018, 11:49

Yes, gregster. thank you for pointing out that the doc says "Contains an array of command line parameters." I missed that. And thanks for the history. Now, I wish I could get the answer to my question. (Actaully, two questions if you tell my an array is not a variable.)
gregster
Posts: 9113
Joined: 30 Sep 2013, 06:48

Re: A_args and subroutines  Topic is solved

27 Feb 2018, 11:51

Why does the MSGBOX display show null values for A_args[1], A_args[2] etc.?
...
Please don't tell me A_args is not a variable. If you do, then: why is it listed as a variable when I open TEST.ahk and view it's variable?
Every AHK script has an A_args variable (as an array). If you don't use command line parameters to run the script, the variable A_args still exists, but just stays blank; msgbox % A_Args.Length() returns "0" when there are no paramters.

The same answer applies to both questions, I believe. A function call doesn't create command line parameters by itself.

Compare https://autohotkey.com/v2/v2-changes.htm :
Command-line args are stored in an array and assigned to the super-global var A_Args instead of a pseudo-array of numbered global vars. If no args were passed to the script, A_Args contains an empty array. The expression A_Args.Length() returns the number of args and A_Args[1] returns the first arg
joefiesta
Posts: 502
Joined: 24 Jan 2016, 13:54
Location: Pa., USA

Re: A_args and subroutines

27 Feb 2018, 13:07

@gregster thanks again for not being offended when I said I just want an answer.

I was so hung up on the FUNCTION not seeing parameters in A_args that I forgot to test the SCRIPT with parameters.

so, I got my answer: I was dumb.

thanks again

Joe P
gregster
Posts: 9113
Joined: 30 Sep 2013, 06:48

Re: A_args and subroutines

27 Feb 2018, 13:30

No problem, Joe! I am glad if I could help.
jonathan scott james
Posts: 42
Joined: 05 Apr 2016, 01:56

Re: A_args and subroutines

03 May 2018, 01:02

why is this script non functional ? both error : : ==> Call to nonexistent function.

Code: Select all

if instr(a_args(a),/params    )
framecycle:=substr (a_arg(1),instr(a_args(a),/params    )+7)
specifically

Code: Select all

a:=1
if instr (A_Args(a),zoom)
framecycle:=substr (a_arg(a),instr(a_args(a),/zoom      )+5)
gregster
Posts: 9113
Joined: 30 Sep 2013, 06:48

Re: A_args and subroutines

03 May 2018, 01:25

Couple of problems here.A_args is an array. Hence, you use square brackets [ ] to access its elements. If you use parentheses, AHK will look for a function called A_Args() instead, which doesn't exist.
You are looking for the literal string /params, I assume - as a parameter of a function (like here with Instr()), you need enclose it in quotation marks --> "/params".
Finally, there are no spaces allowed between a function name and its parentheses:Instr() --> ok, Instr () --> doesn't work.

Not tested:

Code: Select all

a:=1
if instr(a_args[a],"/params")
framecycle := substr(a_arg[1],instr(a_args[a],"/params")+7)
Btw, for such a rather unrelated question, you better start its own topic next time. In the 'Ask for Help' subforum (like the other forums), there is a big button called "New Topic" on the left.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], LAPIII, peter_ahk and 301 guests