Only fill in certain function parameters Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
JustinNL
Posts: 28
Joined: 01 Sep 2020, 13:22

Only fill in certain function parameters

Post by JustinNL » 23 Mar 2021, 14:44

Hi guys,

I have a question concerning functions

Lets say I have a function that may contain 10 parameters like this

Code: Select all

Function(var1="",var2="",var3="",(...),var10="") {
}
And I would like to run the script only filling in variables 2 and 5

Then I could do

Code: Select all

^t::Function(,"99",,,"105")  ; omitting the variables 6-10
But is there also a possibility that fill in the variables in a different way, something like

Code: Select all

^t::Function(var2:="99",var5:="105")
The rest of the variables can be empty, thats no problem
This would make it much easier and a bit more fool-proof for my use.

Is this possible in some way?

Thanks in advance!
Justin
User avatar
boiler
Posts: 17387
Joined: 21 Dec 2014, 02:44

Re: Only fill in certain function parameters

Post by boiler » 23 Mar 2021, 15:08

It doesn't work that way in general, but if you are writing the function (or modifying someone else's), it would be possible for you to set it up so that each parameter would include its name and value as you showed, and the function would parse each parameter to determine which variable it belongs to and what its value is.
User avatar
boiler
Posts: 17387
Joined: 21 Dec 2014, 02:44

Re: Only fill in certain function parameters

Post by boiler » 23 Mar 2021, 15:15

If you did want to use the above approach, here are two ways to do it.

Separate parameters, which the function would parse each of:

Code: Select all

^t::Function("var2=99","var5=105","var12='hello'")

One parameter as one long string, and the function would parse it:

Code: Select all

^t::Function("var2=99,var5=105,var12='hello'")
User avatar
boiler
Posts: 17387
Joined: 21 Dec 2014, 02:44

Re: Only fill in certain function parameters

Post by boiler » 23 Mar 2021, 15:35

An example of how this could work. It might seem like too much overhead for this simple example, but the payoff would be greater for more variables.

Code: Select all

Car("color=black", "model=RX-7", "make=Mazda")
return

Car(var1="", var2="", var3="", var4="") {
	; variables regardless of above order are: make, model, year, color
	loop, 4 {
		e := StrSplit(var%A_Index%, "=")
		if e.1 {
			thisVar := e.1
			%thisVar% := e.2
		}
	}
	MsgBox, %	"Make:`t" make "`n"
		.		"Model:`t" model "`n"
		.		"Year:`t" year "`n"
		.		"Color:`t" color
}
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: Only fill in certain function parameters  Topic is solved

Post by swagfag » 23 Mar 2021, 16:44

Code: Select all

^t::Function({var2: "99", var5: "105"}*)
works for user defined functions only
teadrinker
Posts: 4412
Joined: 29 Mar 2015, 09:41
Contact:

Re: Only fill in certain function parameters

Post by teadrinker » 23 Mar 2021, 16:47

There is one way:

Code: Select all

Function({1: "first", 3: "third", 5: "fifth"}*)

Function(var1 := "", var2 := "", var3 := "", var4 := "", var5 := "") {
   Loop 5
      str .= "var" . A_Index . ": " . var%A_Index% . "`n"
   MsgBox, % str
}
JustinNL
Posts: 28
Joined: 01 Sep 2020, 13:22

Re: Only fill in certain function parameters

Post by JustinNL » 24 Mar 2021, 09:38

Thank you all for you suggestions!

I changed my function using this method. Right now it doesn't matter which variables I fill and in which order, works everytime.
Post Reply

Return to “Ask for Help (v1)”