19

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

Graham
  • 7,807
  • 20
  • 69
  • 114

1 Answers1

33

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 calculate
z := 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.

MCL
  • 3,985
  • 3
  • 27
  • 39
  • 1
    For functions: `myFunction(stringA "123" stringB)` or `myFunction(stringA . "123" . stringB)` – Stevoisiak Aug 23 '17 at 16:59
  • There is special case of concatenating string array, please see https://stackoverflow.com/questions/62608368/how-to-concatenate-a-string-array-in-auto-hotkey – Arunas Bartisius Jun 27 '20 at 09:51