Merge groups of text files randomly preserving order Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Sweetins
Posts: 110
Joined: 02 Jul 2017, 13:22

Merge groups of text files randomly preserving order

05 Apr 2020, 07:43

Textgroup_1.PNG
Textgroup_1.PNG (4.43 KiB) Viewed 797 times
Textgroup_2.PNG
Textgroup_2.PNG (4.23 KiB) Viewed 797 times
Hi everyone, so I have two groups of text files (each group having the same number of text files) that I want to merge into two separate files (one for each group) but both merged files should be randomized in the same way (link-1).
Merged_files.PNG
Merged_files.PNG (9.16 KiB) Viewed 797 times
Also, the order of the contents in each text file (ie. lines) in either group must be preserved in the merged file (link-2). Any ideas on how to achieve this?
User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Merge groups of text files randomly preserving order

05 Apr 2020, 20:45

This generates the two matched shuffled lists, which I understand to be file names, so then you can read the text in from the files per the defined order of file names.

Code: Select all

Text1 =
(
T1 House
T1 Vehicle
T1 Ball
T1 Pencil
)
Text2 =
(
T2 Red
T2 Car
T2 Big
T2 Blunt
)
Text3 =
(
T3 Tree
T3 Cloth
T3 Table
T3 Book
)
Text4 =
(
T4 Tall
T4 Clean
T4 Short
T4 Old
)
AText := Text1 "`n" Text2
BText := Text3 "`n" Text4
A := StrSplit(AText, "`n", "`r")
B := StrSplit(BText, "`n", "`r")
Order := Merge(A.MaxIndex())
loop, % Order.MaxIndex()
	List1 .= A[Order[A_Index]] "`n"
loop, % Order.MaxIndex()
	List2 .= B[Order[A_Index]] "`n"
MsgBox, % List1 "`n`n" List2
return

Merge(n) {
	n /= 2
	c1 := 0
	c2 := 0
	o := []
	loop {
		Random, r, 1, 2
		o.Push(r = 1 ? ++c1 : ++c2 + n)
	} until (c1 = n) || (c2 = n)
	loop, % n - c1
		o.Push(++c1)
	loop, % n - c2
		o.Push(++c2 + n)
	return o
}
Sweetins
Posts: 110
Joined: 02 Jul 2017, 13:22

Re: Merge groups of text files randomly preserving order

06 Apr 2020, 16:24

boiler wrote:
05 Apr 2020, 20:45
This generates the two matched shuffled lists, which I understand to be file names, so then you can read the text in from the files per the defined order of file names.

Code: Select all

Text1 =
(
T1 House
T1 Vehicle
T1 Ball
T1 Pencil
)
Text2 =
(
T2 Red
T2 Car
T2 Big
T2 Blunt
)
Text3 =
(
T3 Tree
T3 Cloth
T3 Table
T3 Book
)
Text4 =
(
T4 Tall
T4 Clean
T4 Short
T4 Old
)
AText := Text1 "`n" Text2
BText := Text3 "`n" Text4
A := StrSplit(AText, "`n", "`r")
B := StrSplit(BText, "`n", "`r")
Order := Merge(A.MaxIndex())
loop, % Order.MaxIndex()
	List1 .= A[Order[A_Index]] "`n"
loop, % Order.MaxIndex()
	List2 .= B[Order[A_Index]] "`n"
MsgBox, % List1 "`n`n" List2
return

Merge(n) {
	n /= 2
	c1 := 0
	c2 := 0
	o := []
	loop {
		Random, r, 1, 2
		o.Push(r = 1 ? ++c1 : ++c2 + n)
	} until (c1 = n) || (c2 = n)
	loop, % n - c1
		o.Push(++c1)
	loop, % n - c2
		o.Push(++c2 + n)
	return o
}
Thanks boiler, but it doesn't seem to work as expected. On first run my merged lists seem to be okay, just that an empty line preceded each list. On subsequent execution, the MsgBox kept being appended with inconsistent subsequent results. Sometimes there is/are more lines in between list.

I did a bit of modification as a preferred both input (which could contain more that four lines) and output files to read and written to, respectively

Code: Select all

q::
FileRead, Text1, C:\Users\Text1.txt
FileRead, Text2, C:\Users\Text2.txt
FileRead, Text3, C:\Users\Text3.txt
FileRead, Text4, C:\Users\Text4.txt
AText := Text1 "`n" Text2
BText := Text3 "`n" Text4
A := StrSplit(AText, "`n", "`r")
B := StrSplit(BText, "`n", "`r")
Order := Merge(A.MaxIndex())
loop, % Order.MaxIndex()
	List1 .= A[Order[A_Index]] "`n"
loop, % Order.MaxIndex()
	List2 .= B[Order[A_Index]] "`n"
FileDelete, C:\Users\ADDAI\Desktop\Aascndn\L1.txt
FileDelete, C:\Users\ADDAI\Desktop\Aascndn\L2.txt
FileAppend, %List1%, C:\Users\L1.txt
FileAppend, %List2%, C:\Users\L2.txt
return

Merge(n) {
	n /= 2
	c1 := 0
	c2 := 0
	o := []
	loop {
		Random, r, 1, 2
		o.Push(r = 1 ? ++c1 : ++c2 + n)
	} until (c1 = n) || (c2 = n)
	loop, % n - c1
		o.Push(++c1)
	loop, % n - c2
		o.Push(++c2 + n)
	return o
}
Same problem. Any ideas?
Last edited by Sweetins on 06 Apr 2020, 19:39, edited 1 time in total.
User avatar
boiler
Posts: 16925
Joined: 21 Dec 2014, 02:44

Re: Merge groups of text files randomly preserving order  Topic is solved

06 Apr 2020, 16:42

You didn't apparently didn't actually run the script as I presented it more than once because it would have cleared out the variables each time you ran it and showed the correct output each time. It looks like you added a hotkey to keep executing the same code, in which case you would have to clear out the List1 and List2 variables, like this:

Code: Select all

q::
List1 := ""
List2 := ""
Text1 =
(
T1 House
T1 Vehicle
T1 Ball
T1 Pencil
)
Text2 =
(
T2 Red
T2 Car
T2 Big
T2 Blunt
)
Text3 =
(
T3 Tree
T3 Cloth
T3 Table
T3 Book
)
Text4 =
(
T4 Tall
T4 Clean
T4 Short
T4 Old
)
AText := Text1 "`n" Text2
BText := Text3 "`n" Text4
A := StrSplit(AText, "`n", "`r")
B := StrSplit(BText, "`n", "`r")
Order := Merge(A.MaxIndex())
loop, % Order.MaxIndex()
	List1 .= A[Order[A_Index]] "`n"
loop, % Order.MaxIndex()
	List2 .= B[Order[A_Index]] "`n"
MsgBox, % List1 "`n`n" List2
return

Merge(n) {
	n /= 2
	c1 := 0
	c2 := 0
	o := []
	loop {
		Random, r, 1, 2
		o.Push(r = 1 ? ++c1 : ++c2 + n)
	} until (c1 = n) || (c2 = n)
	loop, % n - c1
		o.Push(++c1)
	loop, % n - c2
		o.Push(++c2 + n)
	return o
}
Also, there is no empty line preceding each list.
Sweetins
Posts: 110
Joined: 02 Jul 2017, 13:22

Re: Merge groups of text files randomly preserving order

06 Apr 2020, 20:04

Thanks again boiler, I trust if I want to modify it to read from source files and write write to an output file, my previous modification with List1 :="" and List2 :="" at the beginning will do just fine?
Sweetins
Posts: 110
Joined: 02 Jul 2017, 13:22

Re: Merge groups of text files randomly preserving order

07 Apr 2020, 18:02

Thanks Boiler, much appreciated. And like you rightly said, there isn't an empty line preceding each output list (ie. Using the hardcoded input files). It turns out the empty lines where in the input files I imported. Thanks.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Google [Bot], Joey5 and 229 guests