M
Markus Dehmann
What is the most elegant way to split a text file randomly into n
parts? If the original file contains m lines, the n new file should
each contain about m/n lines, chosen uniformly at random.
Leaving the actual handling of files aside here is a naive solution:
my $n = 10;
foreach my $line (0..9999){
my $r = rand;
for(my $i=$n; $i>=0; --$i){
if($r > $i/$n){
print "Line $line: Print to file $i\n";
last;
}
}
}
Are there other ways to do it? This seems like a very typical perl
problem, so it would be interesting to see what solutions others come
up with!
-Markus
parts? If the original file contains m lines, the n new file should
each contain about m/n lines, chosen uniformly at random.
Leaving the actual handling of files aside here is a naive solution:
my $n = 10;
foreach my $line (0..9999){
my $r = rand;
for(my $i=$n; $i>=0; --$i){
if($r > $i/$n){
print "Line $line: Print to file $i\n";
last;
}
}
}
Are there other ways to do it? This seems like a very typical perl
problem, so it would be interesting to see what solutions others come
up with!
-Markus