AHK技术求助:使用变量提取数组信息返回空值,硬编码可正常获取
问题分析与解决方案
老哥,一眼就瞅出问题出在哪了!你给变量em赋值的方式错啦😎
问题根源
你代码里的这行:
em :=Dave
这里的Dave没加引号,AutoHotkey会把它当成变量名去解析,但你之前根本没定义过叫Dave的变量,所以em实际是空值——用空值去访问ProfileList自然拿不到任何内容。而硬编码的ProfileList["Jim"]是直接用字符串索引,所以能正常返回结果。
修正后的代码
只需要给赋值的字符串加上引号,或者用更简洁的表达式语法访问对象就行:
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
额外小技巧
在AutoHotkey v1里,访问对象属性时,直接用变量名(不用百分号包裹)的表达式语法更直观,比如ProfileList[em].Password就比ProfileList[%em%].Password更清晰,两种写法都能工作,但前者可读性更好哦。
内容的提问来源于stack exchange,提问作者Davesexcel
相关产品推荐
相关产品推荐

