Dear Monks,

I have a key-value hash where all keys and values are codes relating to either apples, oranges or lemons. I want to find how many orange-apple, apple-lemon and lemon-orange pairs i have in the hash. All the codes relating to each fruit group are stored in seperate arrays.

However, I could really use some help on finding the best way to do this. Ideally I would like an output (preferably the same order as is in the hash) stating whether each key & value are apples, oranges or lemons:

e.g.

#e.g. lemon-lemon apple-orange orange-lemon orange-apple
Here is my code so far:
my %hash = map { $keys[$_] => $values[$_] } 0.. $#keys; for (my $i=0; $i<@apple_ids; $i++) { while ( my ($key, $value) = each(%hash) ) { if ($apple_ids[$i] == $key) { print "apple"; } if ($apple_ids[$i] == $value) { print "apple"; } } } for (my $i=0; $i<@orange_ids; $i++) { while ( my ($key, $value) = each(%hash) ) { if ($orange_ids[$i] == $key) { print "orange"; } if ($orange_ids[$i] == $value) { print "orange"; } } } for (my $i=0; $i<@lemon_ids; $i++) { while ( my ($key, $value) = each(%hash) ) { if ($lemon_ids[$i] == $key) { print "lemon"; } if ($lemon_ids[$i] == $value) { print "lemon"; } } }

In reply to help querying a hash by Anonymous Monk

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.