How To Create A Multiline String From A Set Of Variables

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
roonyrooxcess
Posts: 61
Joined: 20 Nov 2020, 21:29

How To Create A Multiline String From A Set Of Variables

11 Jun 2021, 06:48

Hi, Im trying to create a multiline string, but not sure how to ...

Basically using a loop of someort to put the variables from A1=Apples A2=bananas A3=Oranges etc., into a multiline string, so it looks like this ...

Code: Select all

D=
(
Apples 
bananas 
Oranges
)
It needs to use a loop of somesort, as the script has a lot of data ...

Thanks.,
User avatar
Chunjee
Posts: 1499
Joined: 18 Apr 2014, 19:05
Contact:

Re: How To Create A Multiline String From A Set Of Variables

11 Jun 2021, 06:54

probably the most straighfoward is to use a newline character between each one:

Code: Select all

var1 := "Apples"
var2 := "bananas"
var3 := "Oranges"

multiLine := var1 "`n" var2 "`n" var3

msgbox, % multiLine
; => 
; Apples
; bananas
; Oranges
Last edited by Chunjee on 11 Jun 2021, 06:56, edited 1 time in total.
User avatar
boiler
Posts: 17390
Joined: 21 Dec 2014, 02:44

Re: How To Create A Multiline String From A Set Of Variables

11 Jun 2021, 06:55

Using a loop so you can easily add more without having to repeat each variable name:

Code: Select all

A1=Apples
A2=bananas
A3=Oranges

loop, 3
	D .= A%A_Index% "`n"
D := Trim(D, "`n")
MsgBox, % D
The only thing you would have to change if you add more variables is the number of times to loop.
Last edited by boiler on 11 Jun 2021, 06:56, edited 1 time in total.
User avatar
Chunjee
Posts: 1499
Joined: 18 Apr 2014, 19:05
Contact:

Re: How To Create A Multiline String From A Set Of Variables

11 Jun 2021, 07:04

Not sure how you have your data setup but the more sensible way to combine a lot of variables together would be an array.

ahk array object doesn't come with concat or join methods by default; this requires https://chunjee.github.io/array.ahk/#/docs?id=concat and https://chunjee.github.io/array.ahk/#/docs?id=join or similar:

Code: Select all

var1 := "Apples"
var2 := "bananas"
var3 := "Oranges"

multilineString := [].concat(var1, var2, var3).join("`n")
msgbox, % multilineString
; => 
; Apples
; bananas
; Oranges
roonyrooxcess
Posts: 61
Joined: 20 Nov 2020, 21:29

Re: How To Create A Multiline String From A Set Of Variables

11 Jun 2021, 12:51

Thanks Chunjee, could you put that in a loop?
User avatar
Chunjee
Posts: 1499
Joined: 18 Apr 2014, 19:05
Contact:

Re: How To Create A Multiline String From A Set Of Variables

11 Jun 2021, 14:05

roonyrooxcess wrote:
11 Jun 2021, 12:51
Thanks Chunjee, could you put that in a loop?

Code: Select all

loop,
{
	var1 := "Apples"
	var2 := "bananas"
	var3 := "Oranges"

	multilineString := [].concat(var1, var2, var3).join("`n")
	msgbox, % multilineString
}
?

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Rohwedder and 158 guests