#!perl -w
# count_words.pl
# 4-29-2012
# Jeen Bae
# Bio Perl Assignment 1
use strict;
my %word_hash = ();
my @word_array = ();
my $word;
# Count words in files from the command line, spliting on white space
print "reading $ARGV[0]\n";
while ( <> ) {
chomp();
@word_array = split( /\s/ );
foreach $word (@word_array){
$word = lc $word;
$word =~ s/^\W+//;
$word =~ s/\W+$//;
$word_hash{$word}++;
}
if ( eof ) {
print "reading $ARGV[0]\n" if $ARGV[0];
}
}
# Create a frequency hash, show words more frequently used first
foreach my $key (sort {$word_hash{$b} <=> $word_hash{$a}}
(sort keys %word_hash) ) {
print "$key\t$word_hash{$key}\n";
}
exit(0);