Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
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.
Here is my code so far:#e.g. lemon-lemon apple-orange orange-lemon orange-apple
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"; } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: help querying a hash
by duff (Parson) on Apr 03, 2006 at 15:16 UTC | |
|
Re: help querying a hash
by jdporter (Paladin) on Apr 03, 2006 at 15:41 UTC | |
|
Re: help querying a hash
by wazzuteke (Hermit) on Apr 03, 2006 at 19:39 UTC |