[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] Random lines from a File



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?

# Fungal Parataxonomy                   Mycology Information (Mycoinfo)
# Webmaster, Staff Writer      **The World's First Mycology E-Journal**   
# <mailto:webmaster@mycoinfo.com>            <http://www.mycoinfo.com/> 
# Ask me about TAXY <http://www.mycoinfo.com/taxy/>
# PORT MAJORDOMO TO THE MAC! <URL coming soon!>


===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org