#!/usr/bin/perl -w

use strict;

# Just read in a file, then print out in hashed (semi-random) order:
open (FH, "<words") || die "Couldn't open \'words\' for input";
my %wds;                           # Word input array

# Input:
my $ct=0;
while (<FH>) {
    chomp;                         # Remove trailing line endings (needed?)
    $_ =~ tr /A-Z/a-z/;            # Fold to lower case
    $wds{$_} = $_;                 # Will squash any duplicates due to case
    $ct++;
}
close (FH);

print "Got $ct input words\n";

# Output:
open (OH, ">words.hashed") || die "Couldn\'t open \'words.hashed\' for output";

$ct=0;
foreach (keys %wds) {
#foreach (sort keys %wds) {
    print OH $wds{$_} . "\n";
    $ct++;
}
close (OH);

print "Unsorted hash output was $ct words\n";

# For further thought:
# - What would it take for truly random ordered output (hint: see
#   the perlfunc man page entries for "rand" and "srand")
# - What value, if any, is the "chomp" function in this example?
# - Could you add prompts or command-line arguments for input and
#   output file specifications?
# - Why does the hashed output appear to be in random order?
# - If you were to produce sorted output, would it be in the same
#   exact order as the input?  Why or why not? 

exit;
