hey monks!
Say I have two arrays:
@array1 = [ "test", "test2", "test2", "test3", "test4", "test4" ];
@array2 = [ "test", "test", "test2.1", "test4.1", "test4.2", "test4.3"
+ ];
How would I go about matching which items in each array could match?
i.e.
foreach my $item (sort @array1) {
foreach my $found (@array2) {
if (&match_names($item,$found)) {
print "found a match ($item = $found\n";
push(@save, $found);
} # end-if
} # end-foreach
} # end-foreach
foreach my $item (sort @array2) {
foreach my $found (@array1) {
if (&match_names($item,$found)) {
print "found a match ($item = $found\n";
push(@save, $found);
} # end-if
} # end-foreach
} # end-foreach
sub match_names {
# i have this part, its just a simple regex
# BUT this must only match in "one direction": test4 matches test 4.
+1, but test4.1 doesnt match test4
return 1 if ($x =~ /some_regex_on_$y/);
}
To get the result:
@save contains [ "test", "test2", "test4", "test4" ];
because test appears in both arrays once,
the first or second test2 in array1 matches test2.1 in array2
test4 in array1 could match test4.1, test4.2 or test4.3 twice.
Its not the regex matching that I'm having trouble with, its the "duplicate" matches thats stumping me...
At the moment, I'm getting many duplicates for the obvious reason that my code doesnt distinguish if its found a match and ignore it next time round.
Any help would be appreciated.
Cheers,
Reagen
Updated to more accurately present my problem.
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.