The data still isn't really representative though is it!? :P Just store the file with the single entries as a hash:
#!/usr/bin/perl
use strict;
use warnings;
my $first = "single_records.txt";
my $second = "multiple_records.txt";
warn "tie-ing $first...\n";
tie my %hsh, 'Tie::File::AsHash', $first, split => qr/\s+/
or die "Problem tying %hash: $!";
open (my $fh, '<', $second) || die "Failed to open $second : $!" ## al
+ways check for success on fh
while (<$fh>){
my $line = chomp($_);
my ($id,) = split /\s+/, $line; ## capture id
## compare to tied hsh of single records
if (exists$hsh{$id}){
print "$line matched $id : $hsh{$id}\n";
}
}
## tidy up
close $fh || die "Failed to close $second : $!";
untie %hsh;
Or you can do the other thing i suggestted and hold an array ref of all the lines with a certain id, then print ot all the lines when you see the id in the single record file.
#!/usr/bin/perl
use strict;
use warnings;
my $first = "single_records.txt";
my $second = "multiple_records.txt";
open (my $fh, '<', $second) || die "Failed to open $second : $!" ## al
+ways check for success on fh
my %second_records = ();
while (<$fh>){
my $line = $_;
my ($id,) = split /\s+/, $line; ## capture id
push @{ $second_records{$id} }, $line;
}
## tidy up
close $fh || die "Failed to close $second : $!";
## open single record file and compare
open ($fh, '<', $first) || die "Failed to open $first : $!";
while (<$fh>){
my $line = chomp( $_ );
my ($id,) = split /\s+/, $line; ## capture id
if ( exists$second_records{$id} ){
print (join '', "$line matches records:\n", @{ $second_records{$id
+} });
}
}
close $fh || die "Failed to close $first : $!";
Hope this helps.
Just a something something...
|