返回一个全局变量,使用字符串作为变量名

时间:2019-03-15 12:31:14

标签: autohotkey

我有一个名为-1的函数,其主要目的是填充已创建的UI。在我的原始脚本中,我需要检查20多个“编辑控件”,并在其中填充内容。内容存储在数组中。

我面临的问题是我不知道如何“返回”带有内容的全局变量,该变量的名称由字符串组成。

这就像说fillUI()是原始变量。然后,我用myVar1 := "Hello World"指向该变量,然后用varName := "myVar1"对其进行更改,但最后varName := "foobar"仍将是myVar1

这可能吗?

Hello World

1 个答案:

答案 0 :(得分:1)

这是一种方法。 脚本开头的global将在该脚本中使用的所有变量声明为全局变量,因此仅使用local

声明仅在FillUI()函数内部使用的变量。
; Start of script
#SingleInstance Force

FillUI()

Gui, Add, Edit, x12 y9 w350 h30 , %Text_1%
Gui, Add, Edit, x12 y89 w350 h30 , %Text_2%
Gui, Add, Edit, x12 y159 w350 h30 , %Text_3%

Gui, Show, w390 h278, Code testing
return

FillUI() {
    global
    local words := {1:"Hello World",2:"foobar",3:"lorem ipsum"}

    local i := 1
    while(i <= words.MaxIndex()) {


        word := words[i]
     ;I need to return a variable here whose variable name needs to be something like "Text_X" and whose content is %word%
        ; This is in order to fill the UI above
        ;
        ; Is this achievable?
        Text_%i% := word

        i++
    }
}

GuiClose:
    ExitApp
return
相关问题