At 12.40 -0500 1999.03.04, Brian McNett wrote: >This is a very quick & dirty script, which opens a file and prints five >lines from it at random. The target file is a list of email addresses in >this instance, but it could be anything. > >#!perl > >my $myfile = q(some_file); >my @data; >my $length; >my $i; > >open(IN, $myfile) and > (@data = <IN>) and > close(IN) or > die(qq($0: Failed on $myfile: $!\n)); # could use warn() > >$length = @data; # Magic! Assigns the length of the array to the scalar! > >for ($i = 1; $i <= 5; $i++) { > print $data[rand($length - 1)]; > } > >I'm open for any suggestions as to improvements. Can it be made MORE >random? Well, here is a slight improvement. Improvement in that it does not read the whole file into memory at once. #!perl -wl my($file, $lines) = 'Shuck Manual'; open F, $file or die $!; for (1..5) { print get_random_line(); } sub get_random_line { local $.; my $line; seek(F, 0, 0); rand($.) < 1 && (chomp($line = $_)) while <F>; return $line; } __END__ It is an adaptation of the entry in the FAQ and Cookbook. I don't fully understand it, so don't ask me. :) -- Chris Nandor mailto:pudge@pobox.com http://pudge.net/ %PGPKey = ('B76E72AD', [1024, '0824090B CE73CA10 1FF77F13 8180B6B6']) ===== Want to unsubscribe from this list? ===== Send mail with body "unsubscribe" to macperl-request@macperl.org