伪数组的 Autohotkey 变量表达式 - 将字符串转换为数字?

Autohotkey variable expressions for pseudo-arrays - Convert String to Number?

这个问题在post的底部得到了回答。

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

...但其中 none 正在为我工​​作。我只是想从从 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[=54”行收到错误“以下变量名称包含非法字符” =]",所以我尝试:

; 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

谢谢!

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

; 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 是 "test",那么您实际上是在说 something := test(其中某些内容最终等于 test 变量的内容)。

Hth,

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

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 行包含 non-printing 个由 Trim(x) 处理的字符,并且仅使用 SubStr(x,1,5).

从 Left 中获取前几个字符