in reply to Re^4: iterate/traverse Two Hashes at once
in thread iterate/traverse Two Hashes at once
As a general thing it is better to keep a single data structure (even a complicated one) than to try and maintain two related data structures. Apart from anything else it is easier to pass around. However, if the data structure is getting complicated turn it into an object to hide the complexity. As a simple example of the technique the following may be of interest:
use strict; use warnings; my $wordData = bless {}; my $previousWord; while (<DATA>) { 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}{$s +econdWord}; } sub WordCount { my ($self, $word) = @_; return 0 if ! exists $self->{$word}; return $self->{$word}{'!count'}; } __DATA__
given the data from the previous example prints:
Count for 'the' is 5
|
|---|