I need to take a file of alphabetized lines (one line per record) and
randomize it. I thought one way of doing this was to read in the file,
one line at a time, generate a random number and store the line in a
hash with the random number as the key. After all the lines were read
in, I'd output each line, sorted by the key (the random number). I tried
to do this in this program:
www:~ # cat randomize.pl
#! /usr/bin/perl -w
# www:/root/randomize.pl written by Kevin Zembower on 18-Dec-2001 to
randomize line in a file.
# Usually called by:
# ./randomize.pl <ordered.list >randomized.list
while (<>) {
$key = rand;
# print "$key => $_";
%randomarray = ($key => $_);
}
foreach $key (keys %randomarray) {
print "$key => $randomarray{$key}";
}
www:~ #
When I run this program, if I uncomment the first print statement, I
can see it reading in the lines of the file, but the foreach loop only
prints the last line.
What dumb mistake did I make?
Is there an easier/faster/better way of doing this that a perl guru
would use?
Thanks so much for your thoughts. I learn lots through this list.
-Kevin Zembower
-----
E. Kevin Zembower
Unix Administrator
Johns Hopkins University/Center for Communications Programs
111 Market Place, Suite 310
Baltimore, MD 21202
410-659-6139
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]