How to pass a variable number of integers to a function

Get help with using AutoHotkey and its commands and hotkeys
DaveT1
Posts: 83
Joined: 07 Oct 2014, 11:23

How to pass a variable number of integers to a function

05 Aug 2020, 14:39

Hi,

Have searched for possible solutions to this, but no avail, which makes me think I'm having a mis-think - maybe someone could straighten me out?....

In a function I want to be able to use some some passed integers to do some If comparisons. But the number of integers passed to the function will be variable, eg:

SortMyArray(myArray, "1,3,10"), or
SortMyArray(myArray, "1"), or
SortMyArray(myArray, "1,2,3,6,9,10"), etc.

Here, the 'integers' are actually being passed as a string. I don't think there is a way to pass these as integers (given that there are a variable number of integers to pass)?
The function might look like:

Code: Select all

SortMyArray(paArray, piIndices:=0)
{
aTemp := StrSplit(piIndices, ",")
for ArrayIndex, ArrayValue in paArray
	{
		if InStr(piIndices, ArrayIndex)	;option 1 - unsatisfactory:
			;do some stuff here.
		for key1, value1 in aTemp	;option 2 - works, but more effort
			{
				if (ArrayIndex = value1)
					;do the necessary stuff here.
			}
	}
}
If the 'integers' are treated as strings in the function as in Option 1, InStr() is great. But suffers from the problem that "1" will appear in "1", "10", "212", etc. In other words, the 'comparison' is purely as a string.

Option 2 shows the comparison that really needs to be done - ie., comparing the numerical values. But is it right that I have to StrSplit the passed string containing the integers, and then add another for loop to properly carry this out?

I'm sure I'm missing something and there must be an easier way to achieve this.

Very grateful for any thoughts.
User avatar
boiler
Posts: 5912
Joined: 21 Dec 2014, 02:44

Re: How to pass a variable number of integers to a function

05 Aug 2020, 15:05

You can pass a variable number of parameters using a variadic function, demonstrated here:

Code: Select all

SortMyArray(myArray, 1, 2, 3, 6, 9, 10)
return

SortMyArray(paArray, piIndices*) {
	out := "Here are the indices that were passed: "
	for each, index in piIndices
		out .= index . A_Space
	MsgBox, % out
}
DaveT1
Posts: 83
Joined: 07 Oct 2014, 11:23

Re: How to pass a variable number of integers to a function

05 Aug 2020, 16:03

Many thanks @boiler - much appreciated.

I'd heard of Variadic functions, but never known what they meant in practice and hence hadn't connected them with this problem - so thanks for the headsup :clap:

I guess re-formulating as a variadic function means that my list of integers are passed into the function (as indeed I was looking for) as an array, as opposed to me passing a string and then using StrSplit to create the equivalent array within the function. It's good that I've got that straight :D .

Once I have this array of integers in the function, I'm then looking to see if any element of one array, is in the other array - sort of the equivalent for numbers to InStr is for chars. But it seems that this is only possible by testing each element of one array against each element of the other array - in other words, by nesting two loops and doing the if test in the second loop.

Thanks again.
User avatar
Chunjee
Posts: 671
Joined: 18 Apr 2014, 19:05
GitHub: Chunjee

Re: How to pass a variable number of integers to a function

05 Aug 2020, 16:19

incomplete information. What is in myArray and what is the expected output?

;do the necessary stuff here.

Return to “Ask For Help”

Who is online

Users browsing this forum: Google [Bot], hamilbody, RubbeH, Zz_ and 40 guests