mIRC Home    About    Download    Register    News    Help

Print Thread
Joined: Dec 2005
Posts: 1
S
Seiken Offline OP
Mostly harmless
OP Offline
Mostly harmless
S
Joined: Dec 2005
Posts: 1
At the moment, I have a text file with a group of names in it. I need a way to copy the text file, then rearrange the names in the new text file in a random order. I also need to do this so that no name corresponds with the first name (line 1 of text file 1 is not the same as line 1 of text file 2).

Any help? I have a feeling it's simpler than I think it is, but I'm not the best at scripting, anyways.

Joined: Aug 2004
Posts: 7,252
R
Hoopy frood
Offline
Hoopy frood
R
Joined: Aug 2004
Posts: 7,252
I'm going to write this up as an alias, since you didn't specify if this was something you needed to be done from your end, or remotely. By putting it as an alias, it'll be easy to accomodate either.
Code:
 alias resort {
while $lines(file.txt) {
while $readn == $calc(1 + $lines(file2.txt)) {
var %name = $read(file.txt)
}
.write file2.txt %name
.write -dl $readn file.txt
}
echo -a file2.txt created from file.txt
 

Joined: Sep 2003
Posts: 4,230
D
Hoopy frood
Offline
Hoopy frood
D
Joined: Sep 2003
Posts: 4,230
Doesnt seem to work dude frown
i think you needed an extra setting of %name in here
while $lines(file.txt) {
var %name = $read(file.txt)
while $readn == $calc(1 + $lines(file2.txt)) {

Even then it has a logic problem,
I see what your idea was, with the second while loop keep rechoosing a random line until it is not the same line as to be outputed onto, problem is your removing the lines from the source file, so assuming 4 nicks nick1 to nick4 in that order in the source file, it might do the following...
random pick line2 (nick2) place it in the out file and delete line2 (3 lines left in source nick1,3,4)
random pick line2 (nick3) and reject it as its output line2 (3 lines left in source nick1,3,4)
random pick line1 (nick2) place it in the out file and delete line1 (2 lines left in source nick3,4)
random pick line1 (nick3) place it in the out file and delete line1 (1 lines left in source nick4)
random pick line1 (nick4) place it in the out file and delete line1
Result
nick2
nick1
nick3
nick4


I pulled this one out from another script and patched it up to work on its own.

Code:
;
;Usage $randfile(<source file>,<destination file>)
;example !.echo -q $randfile(file.txt,file2.txt)
;
alias randfile {
  if ($lines($1) == 1) { write -c $+(",$2,") $read($1,nt,1) | return }
  ;
  window -c @hw.file1 | window -hn @hw.file1
  window -c @hw.file2 | window -hn @hw.file2
  ;
  filter -fwcn $1 @hw.file1
  var %doing = 1
  while ($line(@hw.file1,0)) {
    var %remaining = $v1
    var %line.inwin = $rand(1,%remaining)
    var %line.infile  = $gettok($line(@hw.file1,%line.inwin),1 ,32)
    var %line.content = $gettok($line(@hw.file1,%line.inwin),2-,32)
    while ((%line.infile == %doing) && (%remaining > 1)) {
      var %line.inwin = $rand(1,%remaining)
      var %line.infile  = $gettok($line(@hw.file1,%line.inwin),1 ,32)
      var %line.content = $gettok($line(@hw.file1,%line.inwin),2-,32)
    }
    dline @hw.file1 %line.inwin
    if (%line.infile != %doing) {
      aline @hw.file2 %line.content
    }
    else {
      var %line.toswap = $rand(1,$calc(%doing - 1))
      aline @hw.file2 $line(@hw.file2,%line.toswap)
      rline @hw.file2 %line.toswap %line.content
    }
    inc %doing
  }
  savebuf @hw.file2 $+(",$2,")
}


* warning , this wont deal with blank lines well at all
* warning , this well randomize the lines, BUT doesnt actually insure the contents are different, ie: if u had a file with 2 identicle lines it is possable one or both may appear in the same spot, but its actually one or both have replaced the other one(s)


Link Copied to Clipboard