如何动态设置此 IniRead?

How do I set this IniRead dynamically?

卡了一段时间,希望有经验的人能帮帮我一下...

现在我正在像这样阅读 Ini 三遍,但希望动态地阅读它以改进我的代码 运行。

IniRead, Alert_price_low, %TrackerFile%, Item, Alert_price_low
IniRead, Alert_price_high, %TrackerFile%, Item, Alert_price_high
IniRead, Alert_checkbox, %TrackerFile%, Item, Alert_checkbox

我在下面创建了这个函数,试图动态读取它,但是 return 什么都没有...

FindIniSettings(TrackerFile, Alert_checkbox)

FindIniSettings(TrackerFile, x){
    x := IniRead, %x%, TrackerFile, Item, %x%
    return x
}

我的 Ini 文件内容是这样的:

[Item]
Alert_price_low=777
Alert_price_high=999
Alert_checkbox=1

谢谢!

这里的问题几乎与新表达式语法的使用有关。
似乎您只使用了已弃用的遗留语法,而函数不是其中的一部分。

所以第一个问题就在这里
FindIniSettings(TrackerFile, Alert_checkbox)
这里您没有使用旧语法,因此要指定您引用的字符串。
FindIniSettings(TrackerFile, "Alert_checkbox")
(我假设 TrackerFile 这里是一个包含一些 iniFileNameOrPath.ini 字符串的变量)
此外,您没有在任何地方存储此函数的 return 值。

第二个问题来了
x := IniRead, %x%, TrackerFile, Item, %x%
首先,命令是遗留的,它们没有 return 那样的值。
您不能使用 := 运算符来获取 return 值。
它们 return 仅通过将输出写入请求的变量来获取值,该变量将在命令的第一个参数中指定。
您指定了要命名的输出变量,无论 x 包含什么。这不好,因为你不可能知道运行时输出变量是什么(没有一些不必要的额外技巧)。
此外,将输出命名为与输入键相同会让人很困惑。不过,那行得通。

所以有两个问题,:= 和第一个 %x%,这里还有一些问题要解决:
, TrackerFile,
命令是遗留的,如上所述,它们在每个参数中专门使用遗留语法(除非文档中另有说明)。
因此,您传递的是文字文本“TrackerFile”,而不是应该存储在名为 TrackerFile.
的变量中的任何字符串 在传统语法中,您可以像以前一样通过将变量包裹在 %% 中来引用变量的内容。也许你只是忘了这里。
但实际上,我建议您尝试习惯放弃遗留语法。所以你 could/should 所做的,是以单个 % 开始的参数,然后是 space。这使得 ahk 解释该参数的表达式,而不是使用遗留语法。在现代表达式语法中,您只需键入变量名称即可引用变量。不需要愚蠢的 %%s。


这是您最终应该得到的固定脚本。作为演示,我将这个示例完全遗留语法免费:

TrackerFile := "test.ini"

returnValueOfThisFunction := FindIniSettings(TrackerFile, "Alert_price_low")
MsgBox, % "Yay, the function returned a value for us:`n" returnValueOfThisFunction

return


FindIniSettings(TrackerFile, key)
{
    ;even specified the string "Item" explicitly as a string
    ;this is not needed, but it just looks right to me
    ;I wouldn't want to specify a legacy parameter there
    ;in the middle of all this
    IniRead, output, % TrackerFile, % "Item", % key
    MsgBox, % "Now the variable named ""output"" contains the result of the above command (" output ").`nWe can now return it to the caller."
    return output
}

所以是的,几乎只是理解遗留语法与表达式语法的问题。
您可以阅读我的 之前关于 %%% .
用法的回答 here's AHK 文档中关于脚本语言和传统与现代表达式的一个很好的页面。