#!/usr/bin/perl
my $input = shift( @ARGV );
my $output = shift( @ARGV );
my %words;
unless ( $output ) {
$output = "result.txt";
}
print "The word count will be stored in $output\n";
open INPUT, "$input" or die "Cannot open $input for read: $!";
open OUT, ">$output" or die "Cannot open $output for write: $!"; 
$count = 0;
@testArray = ();
while ( < INPUT> ) {
      @wordArray = split / /;
			foreach ( @wordArray ) {
							if ( /([a-zA-Z]+)/ ) {													
				      	 $words{$1}++;					
								 ++$count; 
							}
			}
			@wordArray = ();			
}
foreach $word ( reverse sort by_occurences keys %words ) {
			print OUT "* $word occurs $words{$word} time(s) in $input\n";
}
print OUT "\n-> $input contains ", scalar keys %words, " unique words and $count", 
      " words at all!";
close OUT;
sub by_occurences {
		$words{$a} <=> $words{$b};
}