in reply to Re: Hash to count characters
in thread Hash to count characters

Whoa! So many great ideas so fast. You monks really are lifesavers! Here's the final working code! (I'll be sure to use strict and warnings in the future!)
print "Counting from @ARGV \n"; &countWords(); &countChar(); sub countWords() { open DAT, "< @ARGV[0]" or die "Can't open @ARGV : $!"; print "Word Count\n"; while($line = <DAT>){ my @line_words = split(/\W/, $line); foreach my $word (@line_words){ if ($wordCount{$word}){ $wordCount{$word}++; }else { $wordCount{$word}=1; } } } close(DAT); for $word (sort keys %wordCount) { print "$word => $wordCount{$word}\n"; } } sub countChar() { open DAT, "< @ARGV[0]" or die "Can't open @ARGV : $!"; print "Character count\n"; while ($line = <DAT>){ my @line_words = split (//, $line); foreach my $char (@line_words){ if ($charCount{$char}){ $charCount{$char}++; }else { $charCount{$char}=1; } } } for $char (sort keys %charCount) { next unless $char =~ /[a-zA-Z]/; print "$char => $charCount{$char}\n"; } close(DAT); }
<3<3 AJ