use strict; use warnings; my $wordData = bless {}; my $previousWord; while () { chomp; my @parts = split; for my $part (@parts) { my ($word, $punct) = (lc $part) =~ /(\w+)(.*)/; $wordData->AddBigram ($word, $previousWord); if ($word) { $previousWord = $word; } elsif ($punct) { $previousWord = undef; } } } my $first = 'the'; my $second = 'value'; if ($wordData->HaveBigram ($first, $second)) { print "Count for '$first' is ", $wordData->WordCount ($first), "\n"; } sub AddBigram { my ($self, $word, $previousWord) = @_; if ($word) { $self->{$previousWord}{$word}++ if $previousWord; $self->{$word}{'!count'}++; } } sub HaveBigram { my ($self, $firstWord, $secondWord) = @_; return exists $self->{$firstWord} && exists $self->{$firstWord}{$secondWord}; } sub WordCount { my ($self, $word) = @_; return 0 if ! exists $self->{$word}; return $self->{$word}{'!count'}; } __DATA__ #### Count for 'the' is 5