【问题标题】:Autohotkey variable expressions for pseudo-arrays - Convert String to Number?伪数组的自动热键变量表达式 - 将字符串转换为数字?
【发布时间】:2019-10-15 08:37:47
【问题描述】:

此问题已在本文底部得到解答。

我查看了 AHK 论坛中提出这个问题的 6 个不同网页,还有一个关于 SO 的网页:

String to Number using autohotkey

...但没有一个对我有用。我只是想从 StringSplit 函数中获取的字符串中减去一个数字。这是我的代码:

; Assign entry price variable. 
StringSplit, prices, Window1Text, `n
MsgBox, Your entry price is %prices32%.

; Assign Stop Loss variable
SLPrice := %prices32% -= 0.10
MsgBox, Your SLPrice is %SLPrice%.

我在“SLPrice := %prices32% -= 0.10”行收到错误“以下变量名包含非法字符”,所以我尝试:

; Assign entry price variable. 
StringSplit, prices, Window1Text, `n
MsgBox, Your entry price is %prices32%.

; Assign Stop Loss variable
SLPrice = %prices32% - 0.10
MsgBox, Your SLPrice is %SLPrice%.

...我得到输出:

Your SLPrice is 7.450 - 0.10

所以它只是将公式显示为文本字符串,实际上并不进行计算。

想法?谢谢!

更新 为了继续制定这个解决方案,这里是我遇到问题的部分的完整代码,以及正在发生的事情的屏幕截图:

; Get the latest window text to parse values from it
WinGetText, Window1Text, ahk_class WindowsForms10.Window.8.app.0.f96fc5_r9_ad1
MsgBox, The text is: %Window1Text% ; Displays the window get text values
Sleep, 5

; Assign entry price variable.
StringSplit, prices, Window1Text, `n
MsgBox, Your entry price is %prices32%.

; Assign Stop Loss variable
SLPrice := prices32 - 0.10
MsgBox, Your SLPrice is %SLPrice%.

回答 感谢下面的贡献者,我们发现有一个“。”从第一个 MsgBox 弄乱 SLPrice 变量开始,我们将 SLPrice 变量更新为:

SLPrice := SubStr(Trim(prices32), 1, 5) - 0.10 ; to pull the left 5 characters

谢谢!

【问题讨论】:

  • 去掉% 标志(SLPrice = prices32 - 0.10) 有什么不同吗?
  • 它应该,但不是你如何拥有它。这是SLPrice := prices32 - 0.10,因为:= 运算符假定表达式!详情见我的回答。
  • @PGilm 请参阅下面的评论。我想我们已经接近了,但现在第二个 msgbox 并没有真正显示数字?
  • 是的,我的原始帖子在变量中有拼写错误(因为我将其更改为适合自己的测试用例)。然后我编辑了答案(几次)以澄清,当我注意到这个错误时(我使用了prices2)。因此,我在您的评论后添加了一个说明性示例。好吧,然后我有兴趣将它更广泛地用于您从 Window1Text 创建的伪数组,并添加了带有输入框和 伪数组变量表达式的版本,因为这就是为什么 AHK 中有伪数组的原因!
  • @MattWilson 好吧,从第一个 msgbox 开始,有一些东西导致了“。”换行到下一行。也许使用:SLPrice := SubStr(Trim(prices32), 1, 5) - 0.10 拉左 5 个字符?

标签: arrays variables macros autohotkey


【解决方案1】:

你在正确的轨道上。但是,根据我的评论,请注意 := 暗示包含变量表达式的表达式(因此没有环绕 %'s):

; Assign entry price variable. 
StringSplit, prices, Window1Text, `n
MsgBox, Your entry price is %prices32%.

; Assign Stop Loss variable
; Note, the 32 line also includes non printing characters
; so must be trimmed and then we take the left 5 characters
SLPrice := SubStr(Trim(prices32), 1, 5) - 0.10
MsgBox, Your SLPrice is %SLPrice%.

应该这样做。 . .

请注意,使用something := %myvariable% 意味着读取名为myvariable 的变量的内容并将这些内容用作变量名。因此,如果 myvariable 是“测试”,那么您实际上是在说 something := test(其中某些内容最终等于 test 变量的内容)。

第,

根据以下内容进行编辑,这是一个工作示例(但根据后文评论,请参见下文):

Window1Text =
(
25
26
27
28
)

; Assign entry price variable. 
StringSplit, prices, Window1Text, `n
MsgBox, Your entry price is %prices2%.  ;  using only 2nd line (26)

; Assign Stop Loss variable
SLPrice := prices2 - 0.10  ;  again, changed to 2nd line
MsgBox, Your SLPrice is %SLPrice%.  ;  25.900000
clipboard := SLPrice

HTH,

进一步编辑:因为这真的很酷,并说明了它们与伪数组变量表达式的关系的几个概念:

Window1Text =
(
25
26
27
28
)

; Assign entry price variable. 
StringSplit, prices, Window1Text, `n  ;  (prices0 is the number of entries)
InputBox, num,, % "Pick an array number from 1 to " prices0  ;  get the array number
; note the variable expression includes the num variable contents
MsgBox, % "Your entry price is " Trim(prices%num%) "."  ;  depends on array number

; Assign Stop Loss variable
SLPrice := Trim(prices%num%) - 0.10  ;  uses the array number value
MsgBox, Your SLPrice is %SLPrice%.  ;  so depends on the array number
clipboard := SLPrice

对吗?

但请注意,这些测试人员工作起来很轻松。来自 OP 的真实示例是复制文本,第 32 行包含由Trim(x) 处理的非打印字符,并且仅从左侧使用SubStr(x,1,5) 获取前几个字符。

【讨论】:

  • 感谢您的帮助!解释清楚,但我只是好奇,第二个 MsgBox 现在显示文本 Your SLPrice is . 所以消息框不显示整数?如果这是有道理的。我希望它准确地告诉我 SLPrice 数字是多少,以便我知道它正在工作(即,如果有意义的话,它会从入门价格数字中减去 0.10)。非常感谢您的意见!
  • 确保完全复制。我必须进行编辑,因为我的原始测试没有 Window1Text 的 32 行条目,所以我在测试中将其写为 prices2 并最初在我的答案中使用它。我将添加一个测试来显示。否则,AHK是什么版本?我的是最新的。
  • 或者更好的是,另一个工作示例也说明了在表达式中使用变量表达式(即something := %variable% 版本)。
猜你喜欢
  • 1970-01-01
  • 2012-10-17
  • 2022-01-24
  • 1970-01-01
  • 1970-01-01
  • 2018-03-04
  • 1970-01-01
  • 1970-01-01
  • 2018-07-04
相关资源
最近更新 更多