If you were forced to look at the files in the order file 1, file 2, you could store the file 1 data in an hash of arrays, with names as keys. Something like this:

#!/usr/bin/perl use strict; use warnings; my $fileNameA = $ARGV[0]; my $fileNameB = $ARGV[1]; my %outputHash = (); my @line; my $x = 0; open (my $inputA, "<", $fileNameA); while (<$inputA>) { next unless $_ =~ m/\w/; @line = split(" ", $_); push (@{$outputHash{$line[1]}}, $_); } open (my $inputB, "<", $fileNameB); while (<$inputB>) { next unless $_ =~ m/\w/; @line = split(" ", $_); if (exists ${$outputHash{$line[2]}}[0]) { for ($x = 0; $x < @{$outputHash{$line[2]}}; $x += 1) { print STDERR "${$outputHash{$line[2]}}[$x]"; } } }

But this would only work if the names in file 2 are unique. And it's more efficient to do it the opposite way around if possible, as suggested above.

-Michael

In reply to Re: Perl: How to print unmatched data after comparison of two files? by mtmcc
in thread Perl: How to print unmatched data after comparison of two files? by WWq

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.