Hi Angharad,
I originally thought your question was fairly simple, but it turned to have some complexity.
If I understand what you're asking for, it doesn't matter what Ob1code and Ob2code are paired together in each line, just the total group of Ob1code values and Ob2code values. Additionally, I'm assuming (because it's true in your example) that each distinct Ob2 always has the same Ob2code value associated with it, which means you can use a simple hash to save those values.
Here is my solution. Note that I've added 4 lines at the end, since your data set did NOT have any matches for either object1 nor object2. The program merely prints out whether the data matches one or the other of Ob1, or both, or neither; I will leave it to you to assign the results to an appropriate data structure for further processing.
#!/usr/bin/perl
# Strict
use strict;
use warnings;
# Libraries
use Data::Dumper;
# Data structures
my $pobject1 = { '1' => { }, '2' => { } };
my %object2 = ( );
# Main program
# Read all data
while (<DATA>) {
# Process line only if it matches template
if (/^HIT\s+object(\d+)\s+(\S+)\s+object(\d+)\s+(\S+)/) {
my ($ob1, $ob1code, $ob2, $ob2code) = ($1, $2, $3, $4);
# Save ob1/ob2 info
$pobject1->{$ob1}->{$ob1code}++;
$object2{$ob2} = $ob2code;
}
}
#
# Debugging ... Display results thus far
#
printf "(debug) %s\n", Dumper($pobject1);
#
# (debug) $VAR1 = {
# '1' => {
# '563.43.78' => 6
# },
# '2' => {
# '563.43.78' => 6
# }
# };
#
printf "(debug) %s\n", Dumper(\%object2);
#
# (debug) $VAR1 = {
# '6' => '566.2222.11',
# '3' => '123.89.7777',
# '7' => '456.222.1111',
# '9' => '1223.333.111',
# '2' => '453.78.122',
# '8' => '990.7777.66',
# '4' => '111.222.333',
# '13' => '123.89.7777',
# '10' => '123.89.7777',
# '5' => '457.8888.1'
# };
#
# Determine which ob2s match both, or just one of the ob1s
my @ob2keys = keys %object2;
foreach my $ob2 (@ob2keys) {
my $ob2code = $object2{$ob2};
my $match1 = exists $pobject1->{'1'}->{$ob2code};
my $match2 = exists $pobject1->{'2'}->{$ob2code};
printf "Ob2 'object#$ob2' Ob2code '$ob2code': ";
if ($match1 and $match2) {
print "matches BOTH object1 and object2\n";
} elsif ($match1) {
print "matches ONLY object1\n";
} elsif ($match2) {
print "matches ONLY object2\n";
} else {
print "doesn't match either Ob1\n";
}
}
__DATA__
HIT object1 563.43.78 object3 123.89.7777
HIT object1 563.43.78 object10 123.89.7777
HIT object1 563.43.78 object2 453.78.122
HIT object1 563.43.78 object5 457.8888.1
HIT object1 563.43.78 object4 123.89.7777
HIT object1 563.43.78 object6 566.2222.11
HIT object2 563.43.78 object3 123.89.7777
HIT object2 563.43.78 object7 456.222.1111
HIT object2 563.43.78 object8 990.7777.66
HIT object2 563.43.78 object5 457.8888.1
HIT object2 563.43.78 object13 123.89.7777
HIT object2 563.43.78 object9 1223.333.111
HIT object1 453.78.122 object12 111.222.333
HIT object2 566.2222.11 object14 333.222.111
HIT object1 990.7777.66 object12 111.222.333
HIT object2 990.7777.66 object14 333.222.111
s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/
|