in reply to Pick random file and print

Well, how many files do you have in that directory? hundreds? thousands?
The following may not be any faster but will certainly use less memory if you have a large number of files:
my ($dir, $file, $rnd) = ('/home/test/_html/index/', undef, undef); opendir DIR, $dir or die $!; while (1){ $file = readdir DIR; last unless defined $file; next unless $file =~ /^index\.[^.]+\.html/; $rnd = $file; last if rand(10) > 5; } closedir DIR; die "No files found\n" unless defined $rnd; open IN, "<$rnd" or die $!; print while (<IN>); close IN;
If you want to make it faster, consider switching to mod_perl. There isn't much else you can do here.

--perlplexer

Replies are listed 'Best First'.
Re: Re: Pick random file and print
by kappa (Chaplain) on May 07, 2002 at 14:19 UTC
    It should be said, that in this snippet the alternatives are not equally possible (unless there're two of them, of course). rand(10) is greater than 5 in approximately every other case. (Approximately because rand(10) never yields 10.)