Dear monks,
I have an array of ids that may or may not be present in a hash. For each id in the array, I want to print the id in consecutive order, but then print some extra info if it is found in the hash.
I have got my code working so that if the id is present it prints the correct information. However, when I print the values not in the hash, it just prints out all values again ( so in my printed list i get duplicates if a value is present in the hash).
How can I alter my code to print each id only once? Should the elsif bit not pick up everything not in the hash?
Thanks!
for (my $i=0; $i<@ids; $i++) {
my $q;
while ( my ($key, $value) = each(%hash) ) {
#print "$key =>$value\n";
if ($ids[$i] eq $key) {
print "$key => $value => $extra[$i]\n";
}
#if ($ids[$i] ne $key) {
elsif ($ids[$i] ne $key) {
$q = "$ids[$i]\t $extra[$i]\n";
}
}
# PROBLEM IS HERE: IT IS PRINTING ONE OF EACH VALUE, NOT JUST
+THOSE THAT AREN'T IN THE HASH
print "$q";
}
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.