何时在AutoHotKey中使用%或%variable%? [英] When to use % or %variable% in AutoHotKey?

查看:45
本文介绍了何时在AutoHotKey中使用%或%variable%?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经搜索了很多,却没有找到答案.在AutoHotKey中,当在行的开头附近使用单个百分号时,以及在两个百分号之间包含变量时,我不确定是否存在差异.我通常使用反复试验来查找何时使用一种或另一种,我希望有人可以阐明它们之间的区别或解释其实际作用.

以下是一些实际的例子.

示例1:我注意到,如果您有多个变量以及文本,脚本通常会使用前面的百分比.如:

  some_val:=剪贴板循环5msgbox%索引:".A_Index., 多变的:" .some_val 

示例2:我也经常看到这种情况,有时似乎必须使用它.这是真的吗?

  some_variable:=测试要发送的文本"发送,%some_variable% 

解决方案

使用双百分号包装是旧版AHK,基本上不再需要使用它.换个双的唯一理由是在过去会被遗忘,或者有人可能会说它在某些情况下写起来也更方便,或有些东西,但是我没有买它.
旧式语法已替换为表达式语法.
表达式语法更接近其他几种语言的行为.AHK旧式语法确实是一团糟.

所有旧式命令(例如,MsgBox)在每个参数上使用旧式旧式语法(除非另行指定).
如果您在参数的开头指定后跟一个空格,则将强制AHK对该参数上的表达式求值,而不是将其作为旧文本参数读取.
示例:
MsgBox,5 + 5 我们使用的是旧版命令,不是使用和空格开头的参数,因此我们使用的是旧版语法.MsgBox将打印文字文本 5 + 5 而不是 10 .
MsgBox,%5 + 5
再次使用旧命令,但现在我们迫使AHK在此处计算 5 + 5 表达式.
表达式求值的结果将传递到MsgBox命令,并且MsgBox将打印 10 .
如果我们想让MsgBox打印文字文本 5 + 5 ,并使用表达式语法来执行此操作,我们将执行 MsgBox,%"5 + 5" .
表达式语法中的引号表示我们正在指定一个字符串.

那么,存在一个问题,即知道何时使用表达式语法以及何时使用旧式语法.
默认情况下,您基本上总是在表达式中.
您可以通过例如使用命令或 = 进行分配来保留它.
如果您不清楚命令和函数之间的区别,请参考以下示例:
命令,%7 + 3,%MyCoolArray [4],%SomeOtherNiceFunction(),%false
Function(7 + 3,MyCoolArray [4],SomeOtherNiceFunction(),false)
在命令中,我们指定了,后跟一个空格以评估每个参数上的表达式,在函数中,由于我们已经在表达式中,因此不必这样做./p>

如果您不清楚 = :=
= 是旧版本,已弃用,它将纯文本分配给变量
:= 将表达式的结果分配给变量.

这就是我可以从头顶上写下的内容.
如果您有一些更复杂的示例,我可以尝试在它们上显示.也许将您可能已经掌握的一些代码转换为表达式语法,使其100%不使用旧式语法.

这是您应该阅读的有关文档的好页面:
https://www.autohotkey.com/docs/Language.htm

I have searched around quite a bit and have failed to find an answer. In AutoHotKey, I am not sure the difference when a single percent is used near the beginning of a line, and when a variable is enclosed between two percent signs. I usually use trial and error to find when I use one or the other, I am hoping someone could shed some light on what the difference is or explain what it is actually doing.

Here are some examples of this in action.

Example 1: I noticed if you have multiple variables along with text, scripts tend to go with the preceding percent. Such as:

some_val := Clipboard

loop 5
    msgbox % "Index:" . A_Index . ", variable:" . some_val 

Example 2: I often see this as well, and sometimes it appears it must be used. Is this true?

some_variable := "test text to send"

Send, %some_variable%

解决方案

Wrapping in double percent signs is legacy AHK and basically there is no need to ever use it anymore. Only reason to wrap in double % would be being stuck behind in the old times, or maybe one could argue it also being more convenient, or something, to write in some cases, but I'm not buying it.
The legacy syntax is replaced by expression syntax.
The expression syntax is closer to how many other languages behave. AHK legacy syntax really is a mess.

All legacy commands (MsgBox for example) use the old legacy syntax on each parameter (unless otherwise specified).
If you specify a % followed up by a space at the start of the parameter, you're forcing AHK to evaluate an expression on that parameter instead of reading it as a legacy text parameter.
Example:
MsgBox, 5+5 We're using a legacy command, we're not starting the parameter off with a % and a space, so we're using the legacy syntax. The MsgBox is going to print the literal text 5+5 instead of 10.
MsgBox, % 5+5
Again, legacy command, but now we're forcing AHK to evaluate an expression here, 5+5.
The result of expression's evaluation is going to be passed onto the MsgBox command and the MsgBox is going to print 10.
If we wanted to MsgBox to print the literal text 5+5, and use the expression syntax to do it, we'd do MsgBox, % "5+5".
Quotation marks in expression syntax mean we're specifying a string.

Well then there's the problem of knowing when you're in expression syntax, and when you're in the legacy syntax.
By default, you're basically always in an expression.
You leave it by for example using a command or = to assign.
If the difference between a command and a function isn't clear to you, here's an example:
Command, % 7+3, % MyCoolArray[4], % SomeOtherNiceFunction(), % false
Function(7+3, MyCoolArray[4], SomeOtherNiceFunction(), false)
In the command we specified a % followed up by a space to evaluate the expressions on each parameter, and in the function, we didn't have to do that since we're already in an expression.

And if you're not clear on the difference between = and :=,
= is legacy and deprecated, it assigns plain text to a variable
:= assigns the result of an expression to a variable.

So that's what I could write from on top of my head.
If you had some more complex examples, I could try showing on them. Maybe convert some code you may have over to expression syntax, make it 100% free of legacy syntax.

And here's a good page on the documentation you should give a read:
https://www.autohotkey.com/docs/Language.htm

这篇关于何时在AutoHotKey中使用%或%variable%?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆