AHK - 使用变量检索数组信息
AHK - Use a variable to retrieve array info
我正在尝试使用变量提取数组信息,但结果是空白。
如果我对其进行硬编码,它将显示正确的信息。
ProfileList := {}
ProfileList.Insert("Dave", {Name:"Dave",Password:"Daves Password",Server:"Regina Server"})
ProfileList.Insert("Jim", {Name:"Jim",Password:"Jims Password",Server:"Saskatoon Server"})
em :=Dave
MsgBox, % "Here is the Password for Dave: " ProfileList[%em%].Password
. "`nHere is the Password for Jim: " ProfileList["Jim"].Password
您似乎混淆了遗留语法和表达式语法。
首先让我们看一下您尝试定义变量的位置 em
。
看起来您正在尝试将字符串 Dave
存储在那里。
如果您使用旧语法 em = Dave
,您将使用 =
运算符将文本分配给变量,而变量 em
确实会保存字符串 Dave
。
但是您使用的是表达式语法 em := Dave
(您应该这样做,现在已经不是 2000 年代的第一个十年了)。因此,您正在为变量 em
分配一个表达式。您分配的表达式是 Dave
。 Dave
,作为一个表达式,应该是一个变量。因此,您将变量 Dave
分配给 em
。但是这样的变量不存在,因此您没有为 em
.
分配任何内容(空)
要在表达式语法中将字符串分配给 em
,您需要执行 em := "Dave"
.
然后是第二个问题,再次在表达式中使用遗留语法。
ProfileList[%em%].Password
%variable%
将是引用变量的旧语法方式,但由于我们在表达式中,我们只想做 ProfileList[em].Password
来引用变量。
遗留语法与表达式语法可能令人困惑。文档中的此页面可能会对您有所帮助:
https://www.autohotkey.com/docs/Language.htm
我建议尝试养成从不使用遗留语法的习惯。当然它也会起作用,但最好不要使用它。也许有一天你会想用 AHK v2 编写,然后就没有使用旧语法了。
在 "Dave" 周围使用引号并且没有百分号。这有效:
ProfileList := {}
ProfileList.Insert("Dave", {Name:"Dave",Password:"Daves Password",Server:"Regina Server"})
ProfileList.Insert("Jim", {Name:"Jim",Password:"Jims Password",Server:"Saskatoon Server"})
em :="Dave"
MsgBox, % "Here is the Password for Dave: " ProfileList[em].Password
. "`nHere is the Password for Jim: " ProfileList["Jim"].Password
我正在尝试使用变量提取数组信息,但结果是空白。
如果我对其进行硬编码,它将显示正确的信息。
ProfileList := {}
ProfileList.Insert("Dave", {Name:"Dave",Password:"Daves Password",Server:"Regina Server"})
ProfileList.Insert("Jim", {Name:"Jim",Password:"Jims Password",Server:"Saskatoon Server"})
em :=Dave
MsgBox, % "Here is the Password for Dave: " ProfileList[%em%].Password
. "`nHere is the Password for Jim: " ProfileList["Jim"].Password
您似乎混淆了遗留语法和表达式语法。
首先让我们看一下您尝试定义变量的位置 em
。
看起来您正在尝试将字符串 Dave
存储在那里。
如果您使用旧语法 em = Dave
,您将使用 =
运算符将文本分配给变量,而变量 em
确实会保存字符串 Dave
。
但是您使用的是表达式语法 em := Dave
(您应该这样做,现在已经不是 2000 年代的第一个十年了)。因此,您正在为变量 em
分配一个表达式。您分配的表达式是 Dave
。 Dave
,作为一个表达式,应该是一个变量。因此,您将变量 Dave
分配给 em
。但是这样的变量不存在,因此您没有为 em
.
分配任何内容(空)
要在表达式语法中将字符串分配给 em
,您需要执行 em := "Dave"
.
然后是第二个问题,再次在表达式中使用遗留语法。
ProfileList[%em%].Password
%variable%
将是引用变量的旧语法方式,但由于我们在表达式中,我们只想做 ProfileList[em].Password
来引用变量。
遗留语法与表达式语法可能令人困惑。文档中的此页面可能会对您有所帮助:
https://www.autohotkey.com/docs/Language.htm
我建议尝试养成从不使用遗留语法的习惯。当然它也会起作用,但最好不要使用它。也许有一天你会想用 AHK v2 编写,然后就没有使用旧语法了。
在 "Dave" 周围使用引号并且没有百分号。这有效:
ProfileList := {}
ProfileList.Insert("Dave", {Name:"Dave",Password:"Daves Password",Server:"Regina Server"})
ProfileList.Insert("Jim", {Name:"Jim",Password:"Jims Password",Server:"Saskatoon Server"})
em :="Dave"
MsgBox, % "Here is the Password for Dave: " ProfileList[em].Password
. "`nHere is the Password for Jim: " ProfileList["Jim"].Password