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

In reply to Re^5: iterate/traverse Two Hashes at once by GrandFather
in thread iterate/traverse Two Hashes at once by cthar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.