Let's say I have a text file as below with the delimiter literally SPLIT LINE HERE. How can I split each block of text into a different file or a different variable.
SPLIT LINE HERE
text line #1
text line #2
text line #3
text line #4
SPLIT LINE HERE
text line #1
text line #2
text line #3
text line #4
SPLIT LINE HERE
text line #1
text line #2
text line #3
text line #4
Split a text file based on a delimiter
Re: Split a text file based on a delimiter
Try this, makes the SplitArray which can then be processed in a for-loop
Use this to read a text file, instead of hardcoding
Note because there is a "SPLIT LINE HERE" at the start of the string the first array element will be blank
Code: Select all
Text := "
(
SPLIT LINE HERE
text line #1
text line #2
text line #3
text line #4
SPLIT LINE HERE
text line #1
text line #2
text line #3
text line #4
SPLIT LINE HERE
text line #1
text line #2
text line #3
text line #4
)"
SplitArray := StrSplit(Text, "SPLIT LINE HERE`n")
For index, value in SplitArray ; Loop through each element (accessed as value) in the SplitArray
MsgBox(value)
Code: Select all
Text := FileRead("MyFile.txt") ; Instead of hardcoding
Re: Split a text file based on a delimiter
Building on Banaanae's...
Code: Select all
#SingleInstance
#Requires AutoHotkey v2+
Text := "
(
SPLIT LINE HERE
text line #1a
text line #2a
text line #3a
text line #4a
SPLIT LINE HERE
text line #1b
text line #2b
text line #3b
text line #4b
SPLIT LINE HERE
text line #1c
text line #2c
text line #3c
text line #4c
)"
SplitArray := StrSplit(Text, "SPLIT LINE HERE`n")
MsgBox SplitArray[2]
MsgBox SplitArray[3]
MsgBox SplitArray[4]
Code: Select all
#SingleInstance
#Requires AutoHotkey v2+
Text := "
(
SPLIT LINE HERE
text line #1a
text line #2a
text line #3a
text line #4a
SPLIT LINE HERE
text line #1b
text line #2b
text line #3b
text line #4b
SPLIT LINE HERE
text line #1c
text line #2c
text line #3c
text line #4c
)"
SplitArray := StrSplit(Text, "SPLIT LINE HERE`n")
loop SplitArray.length - 1 ; number of segments, minus one because of the blank one.
{
segment := A_Index + 1 ; +1 to skip the blank one.
FileAppend(SplitArray[segment], "NewFile-" A_Index) ; "FileAppend" creates a new file, if one doesn't already exist.
}
name := "ste(phen|ve) kunkel"
Re: Split a text file based on a delimiter
great replies. thank you @Banaanae and @kunkel321!
The StrSplit function was essential as well as the proper way to loop starting with the second item in the array.
The StrSplit function was essential as well as the proper way to loop starting with the second item in the array.