- load a text file and read each line into an array.
- Then I want to randomly pick a line and output.
My code doesn't seem to work.
I've tried adding sleep's to allow for the time to read the text file.
Code: Select all
::]focus::
; Initialize an empty array
lines := []
; Open the text file for reading
FileRead, fileContent, focus.txt
Sleep 100
; Split the file content into lines
Loop, Parse, fileContent, `n, `r
{
; Append each line to the array
if (A_LoopField != "")
{
lines.push(A_LoopField)
}
}
; Now you have each line stored in the 'lines' array
; You can access individual lines like lines[1], lines[2], etc.
Sleep 100
; Example: Output each line to the MsgBox
; I added the following to test and see if I was reading in the text
for index, line in lines
{
MsgBox Line %index%: %line%
}
; No message box is displayed
; Get a random item from the array
Random, focus_random, 1, % lines.Length()
myTemp := Clipboard
Clipboard := lines[focus_random]
Sleep 200
SendInput ^v
Sleep 200
Clipboard := myTemp
Return
That one if I do it in 2 steps, it works fine: 1. Read the file and store in array. 2. Access the array and choose the data I want.
I don't understand why I can't just do it all in one operation.
This line:
Code: Select all
MsgBox Line %index%: %line%
Thanks.