【问题标题】:Return a global variable, use a string as the variable name返回一个全局变量,使用字符串作为变量名
【发布时间】:2019-03-15 21:01:45
【问题描述】:

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

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

这就像说myVar1 := "Hello World" 是原始变量。然后我用varName := "myVar1" 指向该变量,然后用varName := "foobar" 更改它,但myVar1 最后仍将是Hello World..

这样的事情可能吗?

; 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() {
    words := {1:"Hello World",2:"foobar",3:"lorem ipsum"}

    i := 1
    while(i <= words.MaxIndex()) {
        global variable_name := "Text_" i
        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?
        variable_name := word
        i++
    }
}

GuiClose:
    ExitApp
return

; End of script

【问题讨论】:

    标签: autohotkey


    【解决方案1】:

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

    ; 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
    

    【讨论】:

    • 完美!非常感谢!
    猜你喜欢
    • 2018-04-20
    • 1970-01-01
    • 2012-07-18
    • 2018-02-10
    • 1970-01-01
    • 1970-01-01
    • 2016-04-03
    • 2014-04-12
    • 1970-01-01
    相关资源
    最近更新 更多