in reply to Comparing two columns

Can you post some representative sample data please?

Although it sounds like you would be better off using a hash for lookups, rather than iterating a whole array for every line - see Tie::File::AsHash.
If you have multiple entries for a given id, you might be better rolling your own

push @{ $data{ $split_line[0] } }, $line;

Just a something something...

Replies are listed 'Best First'.
Re^2: Comparing two columns
by Nalababu (Initiate) on Jul 10, 2009 at 12:51 UTC
    I have posted some sample data.

      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...