【发布时间】: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