Your first loop is fine; it reads lines from the first file and puts them in a hash as keys and values. Your second loop is kind of a mess. You have a few choices:

  1. On each line, find its key in the hash and go ahead and print the key (Italian), the value already in the hash (Spanish), and the value found in the current file (French).
  2. On each line, save the key and value into a new hash. Then after the second loop, have a third loop that goes through one of the hashes and prints out the keys and their values from each hash.
  3. Instead of saving the values in two hashes as simple scalars, save them in a single hash as a two-element array. So the hash would be structured like this:
    $hash = ( uno => [ 'uno','un' ], due => [ 'dos','due' ], tre => [ 'tres','tris' ], # and so on );
    This would mean changing your first loop so that it stores keys and values as $hash{$key}[0] and those from the second loop as $hash{$key}[1].

A problem with solutions #2 and #3 is that a hash is not ordered, so when you loop through the hash to print out the lines, they will not be in the order you want. To fix that, you would have to use an array of arrays instead of a hash, or keep a separate array of the keys to hold their order, or use a module that provides an ordered hash. If you use solution #1, you'll be printing them out in the same order you find them in the second file, which appears to be what you want.

Aaron B.
Available for small or large Perl jobs and *nix system administration; see my home node.


In reply to Re: Need advice on checking two hashes values and keys by aaron_baugher
in thread Need advice on checking two hashes values and keys by perlynewby

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.