If your list is short, using
grep on an array is fine. But this means that you are reading the whole array (second list) each time, which is quite inefficient if the list is long. In that case, using a hash for the second list is a better solution. Re-using
toolic's data sample, it could look like this:
use warnings;
use strict;
my @foo = qw/tom steve bill roger bob/;
my %bar = (roger => 99, steve => 56, ted => 88, tom => 54);
for my $name (@foo) {
print "$name : $bar{$name} \n" if exists $bar{$name};
}
which prints:
tom : 54
steve : 56
roger : 99
I understand that you are reading your data from files, so populating a hash is not more complicated than filling an array. You just have to do the split before populating the hash, instead of doing it later.
Actually, even with a short list, I think that I would probably prefer the hash-based solution.
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.