I wasn't quite sure if these ABC's can be numbers of if you just have single text letters. If they are single text letters as your test data shows, then I would keep them as strings and use tr to count the number of matches. An eval is needed on tr because the pattern is a run time variable. The below demonstrates this general technique.
There can be all kind of "yeah but's" and "what if's" when intersecting things. I just took the approach of solving the problem with your test data. If the data is actually more complex than shown, then that changes things.
#!/usr/bin/perl
use warnings;
use Data::Dumper;
my @data=(
"A B C D E F G", "B F G", "D E F",
"A C", "A D E F G", "G",);
my $standard = shift(@data);
print "standard line: 1 => $standard\n";
$standard =~ s/\s+//g;
my %results;
my $original;
my $line_num=2;
foreach (@data)
{
$original{$_}=$line_num++;
$results{$_}= eval "tr/$standard//";
die @$ if $@;
}
my @sorted_keys = sort {$results{$b} <=> $results{$a}
or
$original{$a} <=> $original{$b}
}keys %results;
foreach (@sorted_keys)
{
print "line: $original{$_} => $_ \t: matches $results{$_}\n";
}
__END__
standard line: 1 => A B C D E F G
line: 5 => A D E F G : matches 5
line: 2 => B F G : matches 3
line: 3 => D E F : matches 3
line: 4 => A C : matches 2
line: 6 => G : matches 1
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.