How to fill and read an array

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Perplexed

How to fill and read an array

22 Jun 2018, 10:32

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.
imustbeamoron
Posts: 44
Joined: 18 Aug 2016, 22:56

Re: How to fill and read an array

22 Jun 2018, 11:37

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
User avatar
SpeedMaster
Posts: 494
Joined: 12 Nov 2016, 16:09

Re: How to fill and read an array

22 Jun 2018, 16:11

Perplexed wrote:What am I doing wrong?
Same as above but with your code :|

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

swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: How to fill and read an array

22 Jun 2018, 16:25

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
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: How to fill and read an array

22 Jun 2018, 17:02

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
Perplexed

Re: How to fill and read an array

23 Jun 2018, 00:28

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

Code: Select all

Value := Lines%A_Index%
is also wrong obviously. For this I tried many different things but nothing worked because of the error with the array declaration.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Chunjee, scriptor2016 and 275 guests