Randomly choose 1 text-line from each of 3 lists of text-lines but instead it brings 3 blank text-lines. Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
rx65m
Posts: 19
Joined: 16 Mar 2018, 17:29

Randomly choose 1 text-line from each of 3 lists of text-lines but instead it brings 3 blank text-lines.

Post by rx65m » 22 Dec 2022, 14:09

I have this code to randomly choose one line of text from each of the three lists of text lines within the same file, and copy them together to the clipboard.
It works but not properly because it brings three blank lines instead of three actual text lines.
It seems to me that the issue may be in the way this code is copying the text lines, but I'm not an expert.
Can somebody help me find where the issue is and maybe code it properly to bring the actual text lines instead of the blank ones?
My AutoHotkey is version 1.1.36.02 . Thanks in advance to everyone.

Code: Select all

Random, rand1, 1, 4
Random, rand2, 1, 4
Random, rand3, 1, 4

; Definining 3 lists of text lines:
list1 = 1st text line, 2nd text line, 3rd text line, 4th text line
list2 = 5th text line, 6th text line, 7th text line, 8th text line
list3 = 9th text line, 10th text line, 11th text line, 12th text line

; Selecting randomly one text line from each list:
selectedLine1 := list1[rand1]
selectedLine2 := list2[rand2]
selectedLine3 := list3[rand3]

; Concatenating the 3 selected text lines and copy them to the clipboard:
clipboard = %selectedLine1% `n %selectedLine2% `n %selectedLine3%

; Seeing the result on a message box:
msgbox, Randoms: %rand1%, %rand2%, %rand3%`nSelected lines:`n%selectedLine1%`n%selectedLine2%`n%selectedLine3%

User avatar
flyingDman
Posts: 2848
Joined: 29 Sep 2013, 19:01

Re: Randomly choose 1 text-line from each of 3 lists of text-lines but instead it brings 3 blank text-lines.  Topic is solved

Post by flyingDman » 22 Dec 2022, 14:17

You need to make arrays first. Use StrSplit() to do so.

Code: Select all

Random, rand1, 1, 4
Random, rand2, 1, 4
Random, rand3, 1, 4

; Definining 3 lists of text lines:
list1 = 1st text line, 2nd text line, 3rd text line, 4th text line
list2 = 5th text line, 6th text line, 7th text line, 8th text line
list3 = 9th text line, 10th text line, 11th text line, 12th text line

; Selecting randomly one text line from each list:
selectedLine1 := strsplit(list1,",")[rand1]
selectedLine2 := strsplit(list2,",")[rand2]
selectedLine3 := strsplit(list3,",")[rand3]

; Concatenating the 3 selected text lines and copy them to the clipboard:
clipboard = %selectedLine1% `n %selectedLine2% `n %selectedLine3%

; Seeing the result on a message box:
msgbox, Randoms: %rand1%, %rand2%, %rand3%`nSelected lines:`n%selectedLine1%`n%selectedLine2%`n%selectedLine3%
14.3 & 1.3.7

User avatar
Smile_
Posts: 859
Joined: 03 May 2020, 00:51

Re: Randomly choose 1 text-line from each of 3 lists of text-lines but instead it brings 3 blank text-lines.

Post by Smile_ » 22 Dec 2022, 14:20

Code: Select all

; Definining 3 lists of text lines:
list1 = 1st text line, 2nd text line, 3rd text line, 4th text line
You don't do it like that

It should:

Code: Select all

list1 := ["1st text line", "2nd text line", "3rd text line", "4th text line"]

rx65m
Posts: 19
Joined: 16 Mar 2018, 17:29

Re: Randomly choose 1 text-line from each of 3 lists of text-lines but instead it brings 3 blank text-lines.

Post by rx65m » 22 Dec 2022, 14:24

flyingDman wrote:
22 Dec 2022, 14:17
You need to make arrays first. Use StrSplit() to do so.

Code: Select all

Random, rand1, 1, 4
Random, rand2, 1, 4
Random, rand3, 1, 4

; Definining 3 lists of text lines:
list1 = 1st text line, 2nd text line, 3rd text line, 4th text line
list2 = 5th text line, 6th text line, 7th text line, 8th text line
list3 = 9th text line, 10th text line, 11th text line, 12th text line

; Selecting randomly one text line from each list:
selectedLine1 := strsplit(list1,",")[rand1]
selectedLine2 := strsplit(list2,",")[rand2]
selectedLine3 := strsplit(list3,",")[rand3]

; Concatenating the 3 selected text lines and copy them to the clipboard:
clipboard = %selectedLine1% `n %selectedLine2% `n %selectedLine3%

; Seeing the result on a message box:
msgbox, Randoms: %rand1%, %rand2%, %rand3%`nSelected lines:`n%selectedLine1%`n%selectedLine2%`n%selectedLine3%
Thank you very much flyingDman
It works charmingly!
Much appreciated!


Post Reply

Return to “Ask for Help (v1)”