It would be better to use a better data structure, updating the word counts and totals as you go. Using your existing data structure you have to iterate through everything to get the answers. Seeing you effectively iterated through everything in the first place you could have generated this data at the same time.
Although it may surprise you a hash of hashes might well have been more space efficient. Say you have 100 stories and in the last story you have an unusual word. Perl has to allocate 100 array slots to put the word count for this word at the correct offset. All the preceding elements spring into existence with value undef.
# $freq{lc $word}[$story]++; my @word_count; my $common_to_all; for my $word( keys %freq ) { my $total = 0; my $common = 0; for my $story( 0..$#title ) { next unless $freq{$word}[$story]; $common++; $total += $freq{$word}[$story]; $word_count[$story] += $freq{$word}[$story]; } printf "\n%-10s %d", $word, $total; if ($common == @title) { $common_to_all++; print " (common to all)" ; } } print "\nStory $_ has $word_count[$_] words." for 0..$#title; print "\nThe stories have $common_to_all words in common.\n";
In reply to Re: Add colums and rows
by tachyon-II
in thread Add colums and rows
by lechateau
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |