Autohotkey 使用_索引或变量作为函数参数

Autohotkey 使用_索引或变量作为函数参数,autohotkey,Autohotkey,我已经搜索了互联网/文档,似乎这是不可能的。但是我有大量的变量需要通过函数传递。该功能工作正常 Example of variable list: st1mrks = 94 st2mrks = 34 st3mrks = ... test1 = "student has collected " st1mrks " marks. " test2 = "student has collected " st2mrks " marks. " test3 = "student has collected

我已经搜索了互联网/文档,似乎这是不可能的。但是我有大量的变量需要通过函数传递。该功能工作正常

Example of variable list:
st1mrks = 94
st2mrks = 34
st3mrks = ...

test1 = "student has collected " st1mrks " marks. "
test2 = "student has collected " st2mrks " marks. "
test3 = "student has collected " st3mrks " marks. "
test4 = ...
或者,我尝试使用while

i=0
while (i < totaltestnumber){
     dofunction(test%i%)
     i++
}
i=0
while(i
但这显然行不通


还有其他方法可以做到这一点吗?

让我们看看一些定义:

  • 函数接受参数
  • 必须始终完全定义
    • i、 e.必须提前指定每个参数
    • i、 e.如果有100个变量,则必须定义100个参数
    • 这是因为参数成为函数内部局部变量的副本,因此需要提前完全定义
  • 不是数组。它们是“顺序编号变量的集合”。
    • i、 e.您有100个独立变量
    • ,虽然方便,但也不建议使用
  • 另一方面,数组。它们是一个包含一系列内容的对象。
    • i、 e.您有一个包含100个元素的变量
我看到两种选择:

选项1:

如果您正在使用,并且有100个变量,那么您就不走运了。您必须将所有100个变量定义为需要单独传递的参数。没有简单的方法可以动态地遍历它们。函数和参数不是这样工作的

选项2

更改为使用,而不是不同的变量。这样,您将只通过一个对象。如果我们严格使用您的示例,您可以:

Example of variable list:
st1mrks = 94
st2mrks = 34
st3mrks = ...

test1 = "student has collected " st1mrks " marks. "
test2 = "student has collected " st2mrks " marks. "
test3 = "student has collected " st3mrks " marks. "
test4 = ...


; Create the array, initially empty:
Array := []

Loop % totaltestnumber{
  Array.Push(test%A_Index%)
}

dofunction(Array)

....

dofunction(ByRef Array)
{
    Loop % Array.MaxIndex()
    {
      ...
    }
}
如果从一开始就使用基于对象的数组,则可以进一步优化此代码

Example of variable list:
st1mrks = 94
st2mrks = 34
st3mrks = ...

test1 = "student has collected " st1mrks " marks. "
test2 = "student has collected " st2mrks " marks. "
test3 = "student has collected " st3mrks " marks. "
test4 = ...


; Create the array, initially empty:
Array := []

Loop % totaltestnumber{
  Array.Push(test%A_Index%)
}

dofunction(Array)

....

dofunction(ByRef Array)
{
    Loop % Array.MaxIndex()
    {
      ...
    }
}