M
Martin Kissner
hello together,
I want to choose a number of files from a directory randomly.
This is what I have so far (after reading "perlodc -f "How do I shuffle
an array randomly?"").
-------
#!/usr/bin/perl
use warnings;
use strict;
my $dir = "/path/to/folder";
opendir DH, $dir || die "can not open $dir: $!";
my @files = grep !/^\.\.?$|/ ,readdir DH;
closedir DH;
my @shuffled;
while (@files) {
# the FAQ says this is bad
push(@shuffled, splice(@files, rand @files, 1));
}
for (1..3) {
print pop @shuffled,"\n";
}
-------
I am pretty sure that there is a better an simpler solution.
Any suggestions will be appreciated.
Regards
Martin
I want to choose a number of files from a directory randomly.
This is what I have so far (after reading "perlodc -f "How do I shuffle
an array randomly?"").
-------
#!/usr/bin/perl
use warnings;
use strict;
my $dir = "/path/to/folder";
opendir DH, $dir || die "can not open $dir: $!";
my @files = grep !/^\.\.?$|/ ,readdir DH;
closedir DH;
my @shuffled;
while (@files) {
# the FAQ says this is bad
push(@shuffled, splice(@files, rand @files, 1));
}
for (1..3) {
print pop @shuffled,"\n";
}
-------
I am pretty sure that there is a better an simpler solution.
Any suggestions will be appreciated.
Regards
Martin