Page 1 of 1

Loop through info in a file

Posted: 23 Feb 2017, 07:19
by Drowseth
Hi,

I want to create a script that loops through information in a text file.

The text file looks like this:

Code: Select all

acc1 char1 char2 char3
acc2 char1 char2 char3
And the idea is to pull the first ID on row 1 (acc1), and then loop through the rest of the IDs on the same line (char1,char2,char3). And then move to line 2 and do the same.
Kinda like this, although I know the syntax is incorrect for AHK.

Code: Select all

for 1 to accounts
{
 for 1 to characters
  {
  do something
  }
}
Anyone able to assist in how I can read the text file into a script, and process the IDs in it in such a loop? Thanks in advance.

Re: Loop through info in a file

Posted: 23 Feb 2017, 08:15
by wolf_II
Try this:

Code: Select all

SetWorkingDir, %A_ScriptDir%

; write example file to work with
FileDelete, example.txt
FileAppend, acc1 char1 char2 char3`nacc2 char1 char2 char3`n, example.txt

; read the file
FileRead, Content, example.txt
MsgBox, %Content% ; show content

; process the content
Loop, Parse, Content, `n, `r ; line by line
{
    Loop, Parse, A_LoopField, %A_Space% ; word by word
    {
        MsgBox, %A_LoopField% ; do something
    }
}
I hope that helps.

Re: Loop through info in a file

Posted: 23 Feb 2017, 08:56
by Drowseth
Thanks, I will try that!

Could you please explain what

Code: Select all

`n, `r
does? Do I understand it correctly that `n means new line and is used as the delimiter and `r means page break and is omitted?

Also, if I wanted to return which line in the file I was currently on, how would I put that?

Re: Loop through info in a file

Posted: 23 Feb 2017, 08:58
by jNizM
LoopParse wrote:Specifying `n prior to `r allows both Windows and Unix files to be parsed.

Ref:
commands/LoopParse.htm (see Example #2)

Re: Loop through info in a file

Posted: 23 Feb 2017, 09:29
by wolf_II
Try this:

Code: Select all

SetWorkingDir, %A_ScriptDir%

; write example file to work with
FileDelete, example.txt
FileAppend, acc1 char1 char2 char3`nacc2 char1 char2 char3`n, example.txt

; read the file
FileRead, Content, example.txt
MsgBox, %Content% ; show content

; process the content
Loop, Parse, Content, `n, `r ; line by line
{
    LineNum := A_Index
    Loop, Parse, A_LoopField, %A_Space% ; word by word
    {
        MsgBox, Line #%LineNum%:`t%A_LoopField% ; do something
    }
}
I hope that helps.

Don't forget you can click keywords in the code box to look up the relevant help page.

Re: Loop through info in a file

Posted: 23 Feb 2017, 12:25
by TravisQ
Maybe some of these ideas could be usefull

Code: Select all

SetWorkingDir,%A_ScriptDir%
Content=
(
acc1 char1 char2 char3
acc2 char21 char22 char23
)
Loop,Parse,Content,`n,`r 
{
	RegExMatch(A_LoopField,"([^\s]+)",fword)
	RegExMatch(A_LoopField,"(?<=\s)(.*)",match)
	MsgBox,3,,Line %A_Index% is :`n%fword% `n`n`trun or skip   (press Yes or No)
	IfMsgBox Yes
		Loop,Parse,match,%A_Space% 
		{
			MsgBox,%A_LoopField% ; do something
		}
	IfMsgBox No
		continue
	IfMsgBox Cancel
		break
} 
ExitApp