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

You could use a hash to get from file2, the files parameter, you want and using a while loop, check if these files parameter gotten from file2 exists in file1. if so print the whole line of file1.
Like this:

use warnings; use strict; use Inline::Files; my %hash; while (<DATA2>) { next if /^\s*$/; $hash{ ( split /\s+?/, $_ )[2] } = undef; } while (<DATA1>) { next if /^\s*$/; if (/^.+?\s(.+?)\s/) { print $_ if exists $hash{$1}; } } __DATA1__ ID alan135 /xkr $work(b05bfn00un0c3) / b05bfn00un0c3; #<= b05bfn00un +0d0 Size:5848.270996 ID John06 /ext $work(b05bfn00ld0p7) / b05bfn00ld0p7; #<= b05bfn00ld +0s0 Size:INFINITY ID lily099 /poli $work(b05bfn00ld0p7) / b05bfn00ld0p7; #<= b05bfn00 +ld0s0 Size:INFINITY ID sam012 /pp $work(b05bfn00ld0p7) / b05bfn00ld0p7; #<= b05bfn00l +d0s0 Size:INFINITY ID lily099 /poli $wwrk(b05bfn00ld0p8) / b05bfn00ld0p8; #<= b05bfn00 +ld0s0 Size:INFINITY ID Steve9018 $work(b05bfn00ld0p7) /b05bfn00ld0p7; #<= b05bfn00 +ld0s0 Size:INFINITY __DATA2__ Accept => John06 / ext Max Accept => vivian788 / ppr Maxcap Accept => suzan645 / pp Min Accept => lily099 / poli Max Accept => Nick5670 / uu Max Accept => Anne309 / pej Min

Update:
Awhoosh!!!, Loops, has similar solution in his Update.
If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me
  • Comment on Re: Perl: How to print unmatched data after comparison of two files?
  • Download Code

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:24 UTC
    Hi, Thanks for your code. I applied your code but It prints out matched data. I only need to print those unmatched data shown as expected result. How can I alter the code? Could you please explain further about hash?
      ..I only need to print those unmatched data shown as expected result..
      use negation not like so:
      ... while (<DATA1>) { next if /^\s*$/; if (/^.+?\s(.+?)\s/) { print $_ if not exists $hash{$1}; # note HERE } } ..

      Could you please explain further about hash?
      See perlintro
      If you tell me, I'll forget.
      If you show me, I'll remember.
      if you involve me, I'll understand.
      --- Author unknown to me

        (not necessarily to 2teez) Be aware that (see perlop's precedence table) the use of not here means that the entire clause on the right side is negated. You can also use ! to just negate the next closest term.

        I often give the advice to use (and, or, not) for flow control, and (&&, ||, !) for logic. In this case I am not taking the position that this use of not is incorrect, just that it is important for the OP to be aware of what it is actually saying.

        Update: As ambrus stated in the CB: "How about unless".

        --MidLifeXis

        Thanks a lot 2teez :)