问题
I have the following auto hotkey script:
A:= 5
B := "7"
C := A.B
MsgBox %C%
The third line does not work.
I'm expecting output of "57"
I have tried the following:
C := %A%.%B%
C := (A).(B)
C := (A.B)
C := (%A%.%B%)
C := (%A%).(%B%)
None of which work
Can anyone tell me how to do it?
I'm using version 1.1.09.04
Just updated to latest version 1.1.14.01 and its still the same
回答1:
You have distinguish between expressions (:=
) and "normal" value assigments (=
). Your goal can be met with several approaches, as shown in the following examples:
a := 5
b := 7
x := 6789
; String concatenation
str1 = %a%%b%
; or as an expression
str2 := a b
; or with explicit concatenation operators
str3 := a . b
; Mathematical "concatenation"
; if b has exactly one digit
val1 := a*10 + b
; for any integer
val2 := a * (10**StrLen(x)) + x ; ** is the "power" operator
msgbox, str1 = %str1%`nstr2 = %str2%`nstr3 = %str3%`nval1 = %val1%`nval2 = %val2%
This code will print:
str1 = 57
str2 = 57
str3 = 57
val1 = 57
val2 = 56789
In AHK, all of these methods should be quasi-equivalent: They produce the same kind of output. The mathematical approach marks the variables as numbers, leading to possible trailing zeros, which you may want to Round()
before displaying. The output of our string concatenation can be used as a number as well, since AHK auto-boxes them if neccessary. For example, you could calculatez := str1 - 1
and it would evaluate to 56
.
I personally prefer the mathematical approach, since it will result result in an actual number and not a string, which seems only logical.
来源:https://stackoverflow.com/questions/21381300/how-to-concatenate-a-number-and-a-string-in-auto-hotkey