gpt4 book ai didi

scope - 如何在 AutoHotkey 的热键中声明局部变量?

转载 作者:行者123 更新时间:2023-12-04 03:04:20 29 4
gpt4 key购买 nike

TL:博士;我不能将热键内的变量声明为本地变量,这意味着 tempindex可全局访问。

我最近发现 local variables不能从热键中声明为本地,或者如果它们被用作 for-loop 中的参数.

^j::
local temp = Hello, world! ; Error: This line does not contain a recognized action
Return

SampleFunction() {
for local temp in myArray { ; Error: Variable name contains an illegal character
; do stuff
}
}

这成为 #warn 的问题启用。除非我记得为每个 for 循环使用唯一的变量名,否则我会遇到以下错误:

Warning: This local variable has same name as a global variable. (Specifically: index)



例如:
#warn

^j::
index = 42 ; This *index* variable is global
Return

UnrelatedFunction() {
for index in myArray { ; This *index* variable is local
MsgBox % myArray[index]
}
}

特别是在使用导入时,这会成为一个问题,因为我自己脚本中的变量经常与我导入脚本中的变量发生冲突。

理想情况下,我可以在任何 for 循环之前放置一个本地声明,但如前所述,我无法在热键中执行此操作。

是否可以从热键中将变量声明为本地变量?

最佳答案

我用函数实现了我的热键。函数内的变量默认具有局部作用域,除非它们被声明 global

F1::alpha(10,10)
F2::alpha(20,30)
F3::beta()


alpha(x,y)
{
myvar := x*2 + y
}

beta()
{
myvar := 47
}

关于scope - 如何在 AutoHotkey 的热键中声明局部变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45967741/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com