Concetenate string array

Get help with using AutoHotkey and its commands and hotkeys
laulau
Posts: 35
Joined: 06 Nov 2020, 16:53

Concetenate string array

11 Nov 2020, 13:28

Im trying to concatenate an array but the return value of Concatenate(s1, s2) doesnt seem to be stored in resultString. Iam probably missing something about how AHK works.

Code: Select all

Concatenate(s1, s2) {
	MsgBox, %s1% %s2%
	Return, %s1% %s2%
}

ConcatenateStringArray(stringArray)
{
	MsgBox, % stringArray.Length()
	resultString := ""
	for index, element in stringArray ; 
	{
		resultString := Concatenate(resultString, element)
	}

	Return resultString
}
User avatar
mikeyww
Posts: 4031
Joined: 09 Sep 2014, 18:38

Re: Concetenate string array

11 Nov 2020, 13:39

Return is used with expressions. Change Return %s1% %s2% to Return s1 s2.
laulau
Posts: 35
Joined: 06 Nov 2020, 16:53

Re: Concetenate string array

11 Nov 2020, 13:41

mikeyww wrote:
11 Nov 2020, 13:39
Return is used with expressions. Change Return %s1% %s2% to Return s1 s2.
Thats what im doing right? I still get nothing. Like if returnString always is empty.
gregster
Posts: 5557
Joined: 30 Sep 2013, 06:48

Re: Concetenate string array

11 Nov 2020, 13:50

Works for me:

Code: Select all

arr := ["hello", "world", "!!!"]
msgbox % ConcatenateStringArray(arr)

Concatenate(s1, s2) {
	; MsgBox, %s1% %s2%
	Return, s1 s2
}

ConcatenateStringArray(stringArray)
{
	; MsgBox, % stringArray.Length()
	resultString := ""
	for index, element in stringArray ; 
	{
		resultString := Concatenate(resultString, element)
	}
	Return resultString
}
btw,
https://www.autohotkey.com/docs/commands/Return.htm#Parameters wrote:Known limitation: For backward compatibility and ease-of-use, the following two examples are functionally identical:

Code: Select all

return MyVar
return %MyVar%
But I think that doesn't apply to multiple variables.. better skip the %s
User avatar
mikeyww
Posts: 4031
Joined: 09 Sep 2014, 18:38

Re: Concetenate string array

11 Nov 2020, 13:58

Fair point. The difference is also demonstrated below, where testB works, but testA does not.

Code: Select all

MsgBox, 64, Results, % "A: " testA("a", "b") "`n`nB: " testB("a", "b")

testA(s1, s2) {
 Return %s1% %s2%
}

testB(s1, s2) {
 x = %s1% %s2%
 Return %x%
}
gregster
Posts: 5557
Joined: 30 Sep 2013, 06:48

Re: Concetenate string array

11 Nov 2020, 14:04

I mean, why use a separate function at all (that just concatenates)? Why not use the concat operator .= ?

Code: Select all

arr := ["hello", "world", "!!!", "123456"]
; arr := {2: "world", 1 : "hello "}
msgbox % ConcatenateStringArray(arr)0

ConcatenateStringArray(stringArray)
{
	for index, element in stringArray 
		resultString .= element
	Return resultString
}
Of course, it should also work with a separate function (see code above).
User avatar
boiler
Posts: 6638
Joined: 21 Dec 2014, 02:44

Re: Concetenate string array

11 Nov 2020, 14:06

You may want to keep the separate function for concatenating, but to demonstrate the concatenation operator .= in case you weren't aware, which makes it even easier to collapse it into one function:

Code: Select all

CongnitiveTest := ["person", "woman", "man", "camera", "TV"]
MsgBox, % ConcatenateStringArray(CongnitiveTest)
return

ConcatenateStringArray(stringArray)
{
	for index, element in stringArray ; 
		resultString .= element
	Return resultString
}
Edit: Too slow! Thinking along the same lines. :D
gregster
Posts: 5557
Joined: 30 Sep 2013, 06:48

Re: Concetenate string array

11 Nov 2020, 14:16

boiler wrote:
11 Nov 2020, 14:06
Edit: Too slow! Thinking along the same lines. :D
Sorry :shifty: , but I have to admit, your word array has more historical importance than mine (smth for the history books) :D
User avatar
boiler
Posts: 6638
Joined: 21 Dec 2014, 02:44

Re: Concetenate string array

11 Nov 2020, 14:21

I couldn't resist as a farewell/good riddance. :lol:
User avatar
Chunjee
Posts: 747
Joined: 18 Apr 2014, 19:05
GitHub: Chunjee

Re: Concetenate string array

11 Nov 2020, 14:40

I think outputting an array into a string of all elements is usually called join but /shrug

https://biga-ahk.github.io/biga.ahk/#/?id=join

Code: Select all

A := new biga() ; requires https://www.npmjs.com/package/biga.ahk

stringArr := ["one", "two", "three"]
resultString := A.join(stringArr, "")
msgbox, % resultString
; => onetwothree

https://chunjee.github.io/array.ahk/#/docs?id=join

Code: Select all

; requires https://www.npmjs.com/package/array.ahk
#Include array.ahk\export.ahk

[1, 2, 3].join()
; => "1,2,3"

["Bill", "Ted"].join(" and ")
; => "Bill and Ted"
User avatar
boiler
Posts: 6638
Joined: 21 Dec 2014, 02:44

Re: Concetenate string array

11 Nov 2020, 15:11

Concatenation involves butting the strings together into one continuous string without delimiters. Join typically involves a delimiter including your own function, which your examples demonstrate by only achieving concatenation when specifying a null string as the delimiter.

Return to “Ask For Help”

Who is online

Users browsing this forum: Bing [Bot], boiler, Brokchus, CR3ALITY, HiSoKa, mikeyww and 64 guests