Seperate string list Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
marc873a
Posts: 37
Joined: 23 Jun 2020, 10:49

Seperate string list

Post by marc873a » 26 Jun 2022, 13:19

Hello everyone.

I have a txt file with a bunch of different words separated by a newline.
I want to retrieve the words and separate them into variables that contain x amount of the words. Let's just say 10 words per variable. What is the simplest way to do this?

So far I have thist

Code: Select all

FileRead, text, test.txt
text := StrSplit(text, "`n")
What to do now?

Thanks in advance and have a good day:)
Last edited by marc873a on 26 Jun 2022, 13:30, edited 1 time in total.

User avatar
mikeyww
Posts: 26885
Joined: 09 Sep 2014, 18:38

Re: Seperate string list

Post by mikeyww » 26 Jun 2022, 13:20

You can post a sample text file below.

marc873a
Posts: 37
Joined: 23 Jun 2020, 10:49

Re: Seperate string list

Post by marc873a » 26 Jun 2022, 13:29

This is just a text file with numbers from 1 to 300
Attachments
test.txt
(1.36 KiB) Downloaded 12 times

User avatar
mikeyww
Posts: 26885
Joined: 09 Sep 2014, 18:38

Re: Seperate string list  Topic is solved

Post by mikeyww » 26 Jun 2022, 13:37

Code: Select all

file         = %A_ScriptDir%\test.txt
wordsPerVar  = 10
var         := []
If !FileExist(file) {
 MsgBox, 48, Error, File not found. Aborting.`n`n%file%
 Return
} Else FileRead, text, %file%
While (text > "") {
 word := StrSplit(text, [" ", "`n"], "`r", rest := wordsPerVar + 1), str := ""
 Loop, %wordsPerVar%
  str .= word[A_Index] " "
 var.Push(Trim(str)), text := word[rest]
 MsgBox,, % "String #" var.Count(), % var[var.Count()]
}

marc873a
Posts: 37
Joined: 23 Jun 2020, 10:49

Re: Seperate string list

Post by marc873a » 26 Jun 2022, 14:06

Thanks!
This is definitely above my AHK coding level but it works perfectly.

I'm wondering what the output variables are?
Like is it var(1), var(2), var(3) etc

User avatar
mikeyww
Posts: 26885
Joined: 09 Sep 2014, 18:38

Re: Seperate string list

Post by mikeyww » 26 Jun 2022, 14:10

Code: Select all

MsgBox, % var.1
MsgBox, % var.2
MsgBox, % var.3
Etc.
Explained: Simple arrays

marc873a
Posts: 37
Joined: 23 Jun 2020, 10:49

Re: Seperate string list

Post by marc873a » 26 Jun 2022, 14:19

Perfect. Thanks :D

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

Re: Seperate string list

Post by flyingDman » 26 Jun 2022, 14:39

Or:

Code: Select all

file := A_ScriptDir "\test.txt"
fileread, fl, % file
z  := 10, cnt := 1
while cnt
	fl := strreplace(strreplace(fl,"`r`n", " ",, z-1),"`r`n", "`n",cnt, 1)
msgbox % fl
Last edited by flyingDman on 26 Jun 2022, 18:42, edited 1 time in total.
14.3 & 1.3.7

User avatar
AlphaBravo
Posts: 586
Joined: 29 Sep 2013, 22:59

Re: Seperate string list

Post by AlphaBravo » 26 Jun 2022, 15:35

another way

Code: Select all

x := 10	;  x amount of the words
n := 1, var:=[]
for i, v in StrSplit(text, "`n", "`r")
	var[ m:=Mod(i, x) ? n : n++] .= v (m ? " " : "")

for i, v in var
	MsgBox % "var." i " = " v

Post Reply

Return to “Ask for Help (v1)”