in reply to Re^3: iterate/traverse Two Hashes at once
in thread iterate/traverse Two Hashes at once

Thanks. it gave me new ideas to approach my problem... Actually, what i am doing is: i have two hashes: %bigram_count and %word count in a separate program.

Next is, in another program, from the given input sentence, eg: "this is example" i take "this is" and search in %bigram_count in other program. if this two-word "this is" is found, then i want to access its count from %bigram_count. At this same process of loop, i also want to compare the single-word "this" and access its count from %word_count.

Sorry for repeated and long post. newbie to perl!

Replies are listed 'Best First'.
Re^5: iterate/traverse Two Hashes at once
by GrandFather (Saint) on Oct 17, 2009 at 21:28 UTC

    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

    True laziness is hard work