in reply to Perl: How to print unmatched data after comparison of two files?

Unfortunately some problems have slipped through because your code doesn't use strict; If you did that you'd see that the $line3 variable is undefined, and others lack lexical scoping.

Update:

Felt a bit guilty for the brevity of my original answer, so here is a quick example of code that works to produce the output you were expecting. It may need some tweaking as you see fit

use strict; use warnings; # Load the acceptable ids from file2 into a hash my %ids; open my $idfile, '<', 'file2' or die $!; for (<$idfile>) { $ids{$1} = 1 if m/Accept\s+=>\s+(\S+)/; } close $idfile; # Scan file1 printing each line where the ID matches one found in our +hash open my $datafile, '<', 'file1' or die $!; for (<$datafile>) { chomp; my ($id) = m/ID\s+(\S+)/; print "$_\n" if exists $ids{$id}; } close $datafile;

Output:

ID John06/ext $work(b05bfn00ld0p7)/b05bfn00ld0p7 ; #<= b05bfn00ld0s0 S +ize:INFINITY ID lily099/poli $work(b05bfn00ld0p7)/b05bfn00ld0p7 ; #<= b05bfn00ld0s0 + Size:INFINITY ID lily099/poli $wwrk(b05bfn00ld0p8)/b05bfn00ld0p8 ; #<= b05bfn00ld0s0 + Size:INFINITY

Replies are listed 'Best First'.
Re^2: Perl: How to print unmatched data after comparison of two files?
by WWq (Novice) on Jul 17, 2013 at 06:38 UTC
    Hi Loops, Thanks! =) I am sorry for my mistake. I updated the expected result. I would like to print out those unmatched data. How to print after name matching?

      No worries, you just need to reverse the test that decides which lines to print. One way is to change the if statement to an "unless" statement.

      print "$_\n" unless exists $ids{$id};
        Yes It works perfectly. Thanks for your guidance. =)