Search code examples
perlcountwordsletters

counting letters for each word in a text with Perl


I am trying to write a program wit Perl which should returns the frequency of all words in the file and the length of each word in the file (not the sum of all characters!) to produce a Zipf curve from a Spanish text (is not a big deal if you don't know what a Zipf's curve is). Now my problem is: I can do the first part and I get the frequency of all word but I don't how to get the length of each word! :( I know the command line $word_length = length($words) but after trying to change the code I really don't know where I should include it and how to count the length for each word.

That's how my code looks like until know:

#!/usr/bin/perl
use strict;
use warnings;

my %count_of;
while (my $line = <>) { #read from file or STDIN
  foreach my $word (split /\s+/gi, $line){
     $count_of{$word}++;
  }
}
print "All words and their counts: \n";
for my $word (sort keys %count_of) {
  print "$word: $count_of{$word}\n";
}
__END__

I hope somebody have any suggestions!


Solution

  • You can use hash of hashes if you want to store the length of the word.

    while (my $line = <>) {
        foreach my $word (split /\s+/, $line) {
            $count_of{$word}{word_count}++;
            $count_of{$word}{word_length} = length($word);
        }
    }
    
    print "All words and their counts and length: \n";
    for my $word (sort keys %count_of) {
        print "$word: $count_of{$word}{word_count} ";
        print "Length of the word:$count_of{$word}{word_length}\n";
    }