JonDepp has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I have two text files and have extracted each to its own array. I want to compare these two arrays to each other, line by line, and print the lines in file2 that are not in file1. Here is my code so far:

use strict; use warnings; my @lines; my @lines2; my $line; my $line2; open (OF, "> diff3.txt"); open (F2, "20100331.txt"); open (F, "20100316.txt"); while (<F>) { @lines=(<F>); } close (F); while (<F2>) { @lines2 = (<F2>); } close (F2); foreach $line (@lines) { foreach $line2 (@lines2) { print OF $line2 if ($line2 ne $line +)} $line=""; $line2=""; } close (OF);

Thanks in advance!!

Replies are listed 'Best First'.
Re: Comparing Two arrays to find differences
by almut (Canon) on Apr 05, 2010 at 18:01 UTC

    BTW,

    while (<F>) { @lines=(<F>); }

    is better simply written as

    @lines = <F>;

    (this way, you also wouldn't skip the first line...)

Re: Comparing Two arrays to find differences
by Corion (Patriarch) on Apr 05, 2010 at 17:29 UTC
Re: Comparing Two arrays to find differences
by repellent (Priest) on Apr 05, 2010 at 20:18 UTC
    $ diff 20100316.txt 20100331.txt | perl -ne 's/^< // and print'