When worried about duplicates, use a hash. It's only a slight change to your code.
foreach my $found (@array2) {
if (&match_names($item,$found)) {
print "found a match ($item = $found\n";
$found_one{$found} = 1; # *** changed line
} # end-if
} # end-foreach
} # end-foreach
@save = keys %found_one;
This will give you one each of the matches. They wont be in the same order as they were found, however. If that's important, then you have to add
push(@save, $found) if !exists $found_one{$found};
$found_one{$found} = 1;
inside the loop, instead of doing keys() at the end to load up @save.
Also, as liverpole notes, you have a typo in the assignments for the sample arrays, where you use square brackets instead of parens. The way it's written the code won't work.
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.