Would some kind soul help me out.
I am trying to read a file into an array.
The following code illustrates the problem I'm having.
There are five lines in the "lines.txt" file.
F12::
Lines = []
Loop, Read, d:\lines.txt
{
MsgBox % A_LoopReadLine
Lines.Push(A_LoopReadLine)
}
Loop 5
{
Value := Lines%A_Index%
MsgBox %Value%
}
return
When executed this code shows a message box with the correct line 5 times but after that it shows an empty message box 5 times.
So it reads the lines from the file correctly, but after that it either doesn't store them into the array or I cannot find out how to read the array content.
What am I doing wrong?
Eventually I'll be using the array lines to Send them, but so far I am stuck on retrieving them from the array.
How to fill and read an array Topic is solved
-
- Posts: 44
- Joined: 18 Aug 2016, 22:56
Re: How to fill and read an array
Code: Select all
;read the text file to variable
fName := "AHK HELP.TXT"
FileRead, tFile, % fName
;create your array
txtArray := []
;fill the array with lines of text
loop, parse, tFile, `n
txtArray.push(A_LoopField)
;show contents of array item 3
MsgBox % txtArray[3]
;show each line of txtArray
for each, line in txtArray
MsgBox % line
- SpeedMaster
- Posts: 494
- Joined: 12 Nov 2016, 16:09
Re: How to fill and read an array
Same as above but with your codePerplexed wrote:What am I doing wrong?
Code: Select all
F12::
Lines := [] ;create your array
Loop, Read, d:\lines.txt
{
MsgBox % A_LoopReadLine
Lines.Push(A_LoopReadLine) ;fill the array with lines of text
}
Loop 5 ;show each line of txtArray
{
Value := Lines[A_Index]
MsgBox %Value%
}
return
Re: How to fill and read an array
same as above, but spare yourself a filereading loop
Code: Select all
FileRead, str, D:\lines.txt
Lines := StrSplit(str, "`n")
MsgBox % Lines[3]
for each, line in Lines
MsgBox % line
Re: How to fill and read an array
To handle CRLF-delimited or LF-delimited text:
Code: Select all
Lines := StrSplit(str, "`n", "`r")
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: How to fill and read an array
Thanks everybody. Now I can do what I wanted.
So it seems that my main error was that I used the equals operator instead of the assignment operator when declaring the array.
The array access
is also wrong obviously. For this I tried many different things but nothing worked because of the error with the array declaration.
So it seems that my main error was that I used the equals operator instead of the assignment operator when declaring the array.
The array access
Code: Select all
Value := Lines%A_Index%
Who is online
Users browsing this forum: OrangeCat and 156 guests