I would do it a little different

Obviously you probably done want to push the file into an array if they are large.

use strict; use warnings; use Data::Dumper; my $file_base = '1.TXT'; my $file_filter = '2.TXT'; open ( FILTER, "<$file_filter" ) or die "Die: Could not open $file_filter: $!"; open ( BASE, "<$file_base" ) or die "Die: Could not open $file_base: $!"; my @filterArray = <FILTER>; my @baseArray = <BASE>; close BASE; close FILTER; unless( arrayDiff( \@filterArray , \@baseArray ) ) { print "Success!"; } sub arrayDiff { my $array1 = shift(@_); my $array2 = shift(@_); my %array1_hash; my %array2_hash; # Create a hash entry for each element in @array1 for my $element ( @{$array1} ) { $array1_hash{$element} = @{$array1}; } # Same for @array2: This time, use map instead of a loop map { $array2_hash{$_} = 1 } @{$array2}; for my $entry ( @{$array2} ) { if ( not $array1_hash{$entry} ) { return 1; #Entry in @array2 but not @array1: Differ } } if ( keys %array1_hash != keys %array2_hash ) { return 1; #Arrays differ } else { return 0; #Arrays contain the same elements } }
Results
Monks>perl 1156663.pl Success!
"We can't all be happy, we can't all be rich, we can't all be lucky – and it would be so much less fun if we were. There must be the dark background to show up the bright colours." Jean Rhys (1890-1979)

In reply to Re: Perl File Comparison by crusty_collins
in thread Perl File Comparison by bopibopi

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.