trying to read a text file and assign variables Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
86lidewell
Posts: 3
Joined: 24 Jun 2022, 22:33

trying to read a text file and assign variables

Post by 86lidewell » 24 Jun 2022, 23:19

Hello,
This is my first post here.

My apologies if I use incorrect terminology, I'm not a "programmer" :crazy:

I need to parse a text file (CSV with no header) that is created by an application which I will use to seed my ahk script. The format of the file is:

Code: Select all

textfield1,textfield2,textfield3,textfield4,textfield5,textfield6
textfield1,textfield2,textfield3,textfield4,textfield5,textfield6
textfield1,textfield2,textfield3,textfield4,textfield5,textfield6
The lines in the file need to be read to the end of the file

Code: Select all

loop, read, test.csv
A GUI drop-down then needs to list each "textfield1" as a choice and then assign to that corresponding choice variables from the remaining fields only in that same line chosen.

Code: Select all

Choose:
Gui,Submit, Nohide
Run, http://%textfield1%/
WinWaitActive, ahk_class windowtext,,20
if ErrorLevel
{
    MsgBox, WinWait timed out.
    return
}
else
    Send, textfield1{tab}textfield2{tab}{tab}{enter}
return
Gui Destroy
ExitApp
So if line 20 was chosen, "textfield1" of line 20 (this line number probably needs to be defined too?) ""textfield1" on line 20 would be associated with only the line 20 variables. Right now the number of lines will be less than 30 or so.
(only two variables are needed right now for each line of this proof of concept, but i may need the others in future iterations) or the program that generates the file may add or remove lines and fields from the source file but will always have at least 6 fields and one line.

"textfiels1" will always be unique, but any of the other fields will be the same.

Thanks a bunch for your time. I have not had much success as of yet.

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

Re: trying to read a text file and assign variables

Post by flyingDman » 25 Jun 2022, 01:24

This would be a basic outline of what you are looking for:

Code: Select all

var =
(
a1,b1,c1,d1,e1
a2,b2,c2,d2,e2
a3,b3,c3,d3,e3
a4,b4,c4,d4,e4
)

arr := []
for a,b in strsplit(var,"`n")
	{
	arr.push(strsplit(b,","))
	lst .= strsplit(b,",").1 "|"
	}

gui, font, s12
gui, add, ddl, w200 choose1 altsubmit vsel gselect,% lst
gui, show
return

select:
gui, submit, nohide
msgbox % arr[sel,1] " " arr[sel,3] " " arr[sel,5]      ; only selecting col a,c, and d
return
14.3 & 1.3.7

86lidewell
Posts: 3
Joined: 24 Jun 2022, 22:33

Re: trying to read a text file and assign variables

Post by 86lidewell » 25 Jun 2022, 11:41

flyingDman wrote:
25 Jun 2022, 01:24
This would be a basic outline of what you are looking for:

Code: Select all

var =
(
a1,b1,c1,d1,e1
a2,b2,c2,d2,e2
a3,b3,c3,d3,e3
a4,b4,c4,d4,e4
)

arr := []
for a,b in strsplit(var,"`n")
	{
	arr.push(strsplit(b,","))
	lst .= strsplit(b,",").1 "|"
	}

gui, font, s12
gui, add, ddl, w200 choose1 altsubmit vsel gselect,% lst
gui, show
return

select:
gui, submit, nohide
msgbox % arr[sel,1] " " arr[sel,3] " " arr[sel,5]      ; only selecting col a,c, and d
return
That works! Thanks! How can I use the loop read file function instead of the var list block?

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

Re: trying to read a text file and assign variables

Post by flyingDman » 25 Jun 2022, 12:03

Use fileread to read the entire file at once
14.3 & 1.3.7

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

Re: trying to read a text file and assign variables  Topic is solved

Post by flyingDman » 25 Jun 2022, 12:49

Or:

Code: Select all

arr := []
Loop, Read, test.csv
	{
	arr.push(strsplit(A_LoopReadLine,","))
	lst .= strsplit(A_LoopReadLine,",").1 "|"
	}

gui, font, s12
gui, add, ddl, w200 choose1 altsubmit vsel gselect,% lst
gui, show
return

select:
gui, submit, nohide
msgbox % arr[sel,1] " " arr[sel,3] " " arr[sel,5]      ; only selecting col a,c, and d
return
14.3 & 1.3.7

86lidewell
Posts: 3
Joined: 24 Jun 2022, 22:33

Re: trying to read a text file and assign variables

Post by 86lidewell » 25 Jun 2022, 13:22

Thank you again! I did not understand where to put the loop read and braces. Works as needed now!

Can I buy you a coffee?

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

Re: trying to read a text file and assign variables

Post by flyingDman » 25 Jun 2022, 13:42

Thank you. I am just glad to help others. That's the only reward I need.
14.3 & 1.3.7

Post Reply

Return to “Ask for Help (v1)”