Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
#!perl use strict; use warnings; my $file = "test.txt"; open (IN, $file) || die "$file not found\n"; my @bigram = <IN>; close (IN); chomp @bigram; my %count; my $word; foreach my $sequence (@bigram) { my $count; my @word = split ' ', $sequence; foreach $word(@word) { $count{$word}++; } } foreach $word (sort by_count keys %count) { print "$word occurs $count{$word} times\n"; } #orders the count sub by_count { $count{$b} <=> $count{$a}; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Dividing a file into groups of two words and counting them
by apl (Monsignor) on May 14, 2008 at 16:13 UTC | |
|
Re: Dividing a file into groups of two words and counting them
by toolic (Bishop) on May 14, 2008 at 16:16 UTC | |
|
Re: Dividing a file into groups of two words and counting them
by johngg (Canon) on May 14, 2008 at 20:06 UTC | |
|
Re: Dividing a file into groups of two words and counting them
by pc88mxer (Vicar) on May 14, 2008 at 16:00 UTC | |
|
Re: Dividing a file into groups of two words and counting them
by Anonymous Monk on May 14, 2008 at 16:17 UTC | |
|
Re: Dividing a file into groups of two words and counting them
by locked_user sundialsvc4 (Abbot) on May 15, 2008 at 12:43 UTC |