If you were doing it by hand, what would you do? Don't worry about writing the Perl code, write out the steps you'd give to someone else to perform the compare.
BTW, you should bracket your code with <code> and </code>, to make it readable... | [reply] |
You may want to consult the perlfaq on intersecting arrays.
perldoc -q intersect. | [reply] [d/l] |
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
| [reply] [d/l] |
Hi,
Thanks for your help. But, I thought, I was not clear
in asking my question; sorry for that. I have arrays
of arrays and I want to compare each of the elements
of one array with the elements of the other array.
For example:
@data = (["A", "B", "C", "D", "E", "F", "G"],
["A","C"],
["B","F","G"],
["A","D","E","F","G"],
["D","E","F"],
["G"],
["M", "N", "O"],
["O"],
["M", "N"],
;
for $row ( 0 .. $#data ) {
for $column ( 0 .. $#{$data[$row]} ) {
print "$row $column is $data[$row][$column]\n";
}
}
I understand till here how to access elements of
arrays of arrays.I would like to compare each of the element in the array with the elements of the next array,and depending upon the number of
matches, I would like to print it in the following way. Any help in this regard would be appreciated, as i am trying to get a clear concept of arrays of arrays.
OUTPUT:
A B C D E F G
A D E F G
B E F
A C
G
M N O
M N
O
| [reply] [d/l] [select] |
Sorry. I do not understand your algorithm, ie method to generate the next line from the previous line. Start with some text that explains how to get line 1, 2, 3, 4 etc.
| [reply] |