Randomize lines of two text files in the same order Topic is solved
Randomize lines of two text files in the same order
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.
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?
Re: Randomize lines of two text files in the same order
- 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.
[EDIT:] Here's a more complete example.
- 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
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
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: Randomize lines of two text files in the same order
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
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
Re: Randomize lines of two text files in the same order
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.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
Re: Randomize lines of two text files in the same order
- 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.
- 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
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: Randomize lines of two text files in the same order Topic is solved
Sure, thanks. Am not that much of a pro so if I don't get the explanations, I find it difficult to relate.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.
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.
Re: Randomize lines of two text files in the same order
- It might be good to include a check that each file has the same line count. Otherwise the pairings could get out of sync.
- Note:
Code: Select all
StrReplace(vList1, "`n",, vCount1)
StrReplace(vList2, "`n",, vCount2)
if !(vCount1 = vCount2)
{
MsgBox, % "error: line count mismatch"
return
}
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
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: Randomize lines of two text files in the same order
This will remove any trailing carriage returns/line feeds from the end of the string. There are also Trim and LTrim functions.
Btw do keep backups of the lists! Cheers.
Code: Select all
vList1 := RTrim(vList1, "`r`n")
homepage | tutorials | wish list | fun threads | donate
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
WARNING: copy your posts/messages before hitting Submit as you may lose them due to CAPTCHA
Re: Randomize lines of two text files in the same order
Was wondering, can I extend the application of what is here to the situation explained here? @Jeeswg
Who is online
Users browsing this forum: Bing [Bot], Xeilous and 183 guests