Randomize lines of two text files in the same order Topic is solved

Get help with using AutoHotkey and its commands and hotkeys
Sweetins
Posts: 74
Joined: 02 Jul 2017, 13:22

Randomize lines of two text files in the same order

18 Feb 2019, 06:20

A python-script plugin for notepad++ allows randomizing lines but for a specified text file. However, am trying to randomize two text files with the same number of lines (over hundred lines each) at the same time so that the same order of randomization is maintained as shown in the attached picture.
Figure.PNG
Figure.PNG (13.03 KiB) Viewed 1432 times
That is, the matching words (eg ball = red) for each line must be maintained after the randomization. Is there any way to achieve this with autohotkey?
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Randomize lines of two text files in the same order

18 Feb 2019, 08:20

- Interesting question. Try this. Cheers.
- You use the Random command to specify a seed, an arbitrary number.
- The Random command uses pseudorandom numbers which are based on a seed value.

Code: Select all

q:: ;sort list in a consistent random order
Random,, 1337
vList := "a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z"
;vList := "AA,BB,CC,DD,EE,FF,GG,HH,II,JJ,KK,LL,MM,NN,OO,PP,QQ,RR,SS,TT,UU,VV,WW,XX,YY,ZZ"
;vList := "z,y,x,w,v,u,t,s,r,q,p,o,n,m,l,k,j,i,h,g,f,e,d,c,b,a"
Sort, vList, D, Random
MsgBox, % vList
return
[EDIT:] Here's a more complete example.

Code: Select all

q:: ;sort list in a consistent random order
vList1 := "1,2,3,4,5,6,7,8,9,10,11,12"
vList2 := "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"
Random, vSeed, -2147483648, 2147483647
vSeed += 2147483648
Random,, % vSeed
Sort, vList1, D, Random
Random,, % vSeed
Sort, vList2, D, Random
oArray := StrSplit(vList1, ",")
vOutput := ""
VarSetCapacity(vOutput, StrLen(vList2)*2*2)
Loop, Parse, vList2, % ","
	vOutput .= oArray[A_Index] " " A_LoopField "`r`n"
Clipboard := vOutput
MsgBox, % vOutput
return
Last edited by jeeswg on 18 Feb 2019, 08:47, edited 1 time in total.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
User avatar
nnnik
Posts: 4461
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: Randomize lines of two text files in the same order

18 Feb 2019, 08:34

Yes there is.
You load both files - and seperate the by lines.
This should also allow you to count lines as well.
Now you generate a random number thats lower than the line count.
You take that line from the seperated lines list and move it to a new list with lines.
You repeat this step until all lines are in the new list in a new random order.
As a last step you combine the lists to one piece of text and write it to the disk.

Edit: too late
Recommends AHK Studio
Sweetins
Posts: 74
Joined: 02 Jul 2017, 13:22

Re: Randomize lines of two text files in the same order

18 Feb 2019, 13:10

jeeswg wrote:
18 Feb 2019, 08:20
[EDIT:] Here's a more complete example.

Code: Select all

q:: ;sort list in a consistent random order
vList1 := "1,2,3,4,5,6,7,8,9,10,11,12"
vList2 := "Jan,Feb,Mar,Apr,May,Jun,Jul,Aug,Sep,Oct,Nov,Dec"
Random, vSeed, -2147483648, 2147483647
vSeed += 2147483648
Random,, % vSeed
Sort, vList1, D, Random
Random,, % vSeed
Sort, vList2, D, Random
oArray := StrSplit(vList1, ",")
vOutput := ""
VarSetCapacity(vOutput, StrLen(vList2)*2*2)
Loop, Parse, vList2, % ","
	vOutput .= oArray[A_Index] " " A_LoopField "`r`n"
Clipboard := vOutput
MsgBox, % vOutput
return
Thanks for the quick response Jeeswg. It however looks like I was misunderstood somewhere. The text files file 1 and file 2 are two separate files. The content of each corresponding lines in the separate files are however related. Thus, randomizing must maintain connection between content of the same line number. That is, if line 2 in file 1 and file 2 are house and blue respectively as shown in the unrandomized aspect of the picture, after randomizing, if line 3 of file 1 is house, line 3 in file 2 must also be blue (maintaining the connection after randomizing). Again the two files are separate. I could combine them and randomize a single text file but the challenge is maintaining the two text files separate and orderly randomized. Thanks again.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Randomize lines of two text files in the same order

18 Feb 2019, 16:10

- Hello Sweetins, I believe that I did understand what you meant, and that the script demonstrates how to do it.
- I took 2 lists *with the same number of items*, and sorted both lists so that they were shuffled in the same way. I demonstrated that both lists were shuffled the same way, by combining them (but you don't have to combine them).
- I used 2 hardcoded lists, but you could use FileRead to input 2 lists, and you could use FileAppend to output 2 shuffled lists (and you don't have to combine the lists).

- The key point is that by using Random and a seed value, that resets the pseudorandom numbers, so you get the same random numbers (from Random) / random sort (from Sort) each time.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Sweetins
Posts: 74
Joined: 02 Jul 2017, 13:22

Re: Randomize lines of two text files in the same order  Topic is solved

18 Feb 2019, 21:13

jeeswg wrote:
18 Feb 2019, 16:10
- Hello Sweetins, I believe that I did understand what you meant, and that the script demonstrates how to do it.
- I took 2 lists *with the same number of items*, and sorted both lists so that they were shuffled in the same way. I demonstrated that both lists were shuffled the same way, by combining them (but you don't have to combine them).
- I used 2 hardcoded lists, but you could use FileRead to input 2 lists, and you could use FileAppend to output 2 shuffled lists (and you don't have to combine the lists).

- The key point is that by using Random and a seed value, that resets the pseudorandom numbers, so you get the same random numbers (from Random) / random sort (from Sort) each time.
Sure, thanks. Am not that much of a pro so if I don't get the explanations, I find it difficult to relate.

Code: Select all

q:: ;sort list in a consistent random order
FileRead, vList1, C:\Users\file 1.txt
FileRead, vList2, C:\Users\file 2.txt
StrReplace(vList1, "`n",, vCount1)
StrReplace(vList2, "`n",, vCount2)
if !(vCount1 = vCount2)
{
	MsgBox, % "error: line count mismatch"
	return
	}
	else
{
	Random, vSeed, -2147483648, 2147483647
	vSeed += 2147483648
	Random,, % vSeed
	Sort, vList1, Random
	Random,, % vSeed
	Sort, vList2, Random
	FileDelete, C:\Users\file 1.txt
	FileDelete, C:\Users\file 2.txt
	FileAppend, %vList1%, C:\Users\file 1.txt
	FileAppend, %vList2%, C:\Users\file 2.txt
	return
	}
return
Last edited by Sweetins on 19 Feb 2019, 11:56, edited 2 times in total.
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Randomize lines of two text files in the same order

18 Feb 2019, 21:25

- It might be good to include a check that each file has the same line count. Otherwise the pairings could get out of sync.

Code: Select all

StrReplace(vList1, "`n",, vCount1)
StrReplace(vList2, "`n",, vCount2)
if !(vCount1 = vCount2)
{
	MsgBox, % "error: line count mismatch"
	return
}
- Note:

Code: Select all

StrReplace(vList1, "`n",, vCount1) ;vList1 not modified
vList1 := StrReplace(vList1, "`n",, vCount1) ;vList1 modified
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Sweetins
Posts: 74
Joined: 02 Jul 2017, 13:22

Re: Randomize lines of two text files in the same order

19 Feb 2019, 11:13

jeeswg wrote:
18 Feb 2019, 21:25
- It might be good to include a check that each file has the same line count. Otherwise the pairings could get out of sync.
What if either file ends with one or more empty lines that shouldn't necessarily be counted as part of the total? :think:
User avatar
jeeswg
Posts: 6902
Joined: 19 Dec 2016, 01:58
Location: UK

Re: Randomize lines of two text files in the same order

19 Feb 2019, 11:30

This will remove any trailing carriage returns/line feeds from the end of the string. There are also Trim and LTrim functions.

Code: Select all

vList1 := RTrim(vList1, "`r`n")
Btw do keep backups of the lists! Cheers.
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Sweetins
Posts: 74
Joined: 02 Jul 2017, 13:22

Re: Randomize lines of two text files in the same order

05 Apr 2020, 16:43

Was wondering, can I extend the application of what is here to the situation explained here? @Jeeswg

Return to “Ask For Help”

Who is online

Users browsing this forum: JackMa, rpgfreak128 and 267 guests