#!/usr/bin/perl -w
use strict;

#this is a complete perl code which actually do 
#something useful. 
#you should be able to understand almost everything here
#after your first lecutre of Perl programming. 
#to run this code, do:
#	wc.pl filename
#or, 
#	perl wc.pl filename
#I provided one file to use for this code, wotw.txt.


open(WORDL, $ARGV[0]) || die "can't open wordlist: $!" ;
open(OUT, ">wc_result") || die "can't open wordlist: $!" ;

my(@tmp, @wordlist, $tmp, %wordhash, $first, $second); 

while (<WORDL>) {
	chomp;
	#replace special chars with space...
	s/[^A-Za-z1-9_]/ /g;
	@wordlist = split(/\s+/);
	foreach $tmp ( @wordlist ){
	    #omitt the numbers...
	    if( $tmp =~ /[0-9]+/ ){
		;
	    } 
	    #put them in a hash
	    else{
		if ( ! $wordhash{$tmp} ){
			$wordhash{$tmp} = 1;
		}
		else{
			$wordhash{$tmp} += 1;
		}
	     }
	}
}

foreach (sort keys(%wordhash) ){
	print(OUT "$wordhash{$_} \t $_ \n" );
}	

close(WORDL);
close(OUT);

#system("sort -n -r +1 wc_result | head -30 > wc_out");
#unlink( "wc_result");
