I've got a feeling you don't understand the problem statement. This means you won't understand any of the possible solutions well enough to code them.

Problem Statement: Take the values from two files and find those values that are the same. Print those to a third file.

Problem Solution: Since it seems that you just want to find all values in file 2 that exist in file 1, you don't really care if there are duplicates in either file or not. Existence it the important thing. So, it sounds like hashes are your friend here.

my @file1 = <FILE1>; my @file2 = <FILE2>; # Here is where you would normalize the data. Things like uc, lc, ucfi +rst, s/\s//g, and the like. my (%file1, %file2); $file1{$_} = 1 foreach @file1; $file2{$_} = 1 foreach @file2; foreach my $value (sort keys %file1) { if ($file2{$value}) { print FILE3 "$value\n"; } }
By looking at things this way, you can then easily find out which in file 2 aren't in file 1.
# Using the same data structures as above ... foreach my $value (sort keys %file2) { unless ($file1{$value}) { print FILE3 "$value\n"; } }
Also, this lends itself to a counting of the instances, if you expect duplicates and you care. You can change the hash populating part to
my (%file1, %file2); $file1{$_}++ foreach @file1; $file2{$_}++ foreach @file2;
You could then do a <code>print FILE3 "$value : $file1{$value}\n";

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.


In reply to Re: Comparing two files by dragonchild
in thread Comparing two files by bman

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.