Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Perl File Comparison

by bopibopi (Initiate)
on Mar 02, 2016 at 13:52 UTC ( [id://1156663]=perlquestion: print w/replies, xml ) Need Help??

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

Hey, i ve writted this code for seq text file comparison, for some reason it doesnt seem to work. I know there's been a lot of similar topics, and i ve sort of tried to compile a solution out of all but to no avail. Really new to perl, can someone help me?

my $file_base = 'CSP8216.TXT'; my $file_filter = 'CSP8216.TXT'; open my $info_filter, $file_filter or die "Die: Could not open $file_f +ilter: $!"; while(my $line_filter = <$infofilter>) { open my $info_base, $file_base or die "Die: Could not open $fi +le_base: $!"; while(my $line_base = <$info_base>) { if("$line_filter"=="$line_base") #if(substr($line_filter, 0, 11)==substr($line_base, 0, + 11)) { print $line_base; } } close $info_base; } close $info_filter;

Replies are listed 'Best First'.
Re: Perl File Comparison
by toolic (Bishop) on Mar 02, 2016 at 14:13 UTC
Re: Perl File Comparison
by 1nickt (Canon) on Mar 02, 2016 at 14:12 UTC

    One problem: You have to tell Perl what you want to do with the filehandle you open. To read:

    open my $INFO_FILTER, '<', $file_filter or die "Die: Could not open $f +ile_filter: $!"; while ( <$INFO_FILTER> ) { ... }
    (I give filehandles names in upper case for better readability.)

    Also, use eq, not ==, when comparing strings.

    Hope this helps!


    The way forward always starts with a minimal test.
Re: Perl File Comparison
by ExReg (Priest) on Mar 02, 2016 at 14:44 UTC

    Another possibility is that you may be using the wrong comparison operator. You are using ==, which is used to compare numbers. If you are comparing strings, which it looks like you are, you would want eq .

    Edit: Oops, sorry, I see 1nickt already mentioned that.

Re: Perl File Comparison
by crusty_collins (Friar) on Mar 02, 2016 at 15:34 UTC
    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)
      Hi, You have done a great job with your explanation. The code is clean well commented and I believe solves the problem. What I doubt is if he will understand references cos he is new to perl. My advice will be for him to first of all get comfortable with the basic before attempting to cracking or solve difficult task.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1156663]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2024-03-28 11:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found