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

Hi Monks,
I have to compare two files.I have to check if both file contents are same.But I want an shorter code.Can you give inputs.
come down & check the code,
Input1:(Input file) contains Hi this is the line Here to check date Input2;(Input second file) contains Hi this is the line Here to check date ------------------- #!/usr/local/bin/perl open ipfile1 Input1 or die"File not found $!\n"; open ipfile2 Input2 or die"File not found $!\n"; if(ipfile1 == ipfile2){ print"Both File are Equal"; }else{ print"Not-Equal"; }
Thanks,
qsl.

Replies are listed 'Best First'.
Re: How to compare array with module
by prasadbabu (Prior) on Jul 01, 2006 at 09:58 UTC

    Hi qsl,

    Take a look at File::Compare which ll accomplish your work. Please also do Super Search before posting.

    use strict; use warnings; use File::Compare; my $file1 = ("1.xml"); #first file my $file2 = ("2.xml"); #second file (compare("$file1", "$file2")== 0) ? print "They're equal\n" : print "n +ot equal";

    Prasad

Re: How to compare array with module
by vkon (Curate) on Jul 01, 2006 at 10:00 UTC
    use File::Slurp; my $f1 = read_file('Input1'); my $f2 = read_file('Input2'); if ($f1 eq $f2) { print"Both File are Equal"; }else{ print"Not-Equal"; }
    '==' is a number comparision operator, and its wrong to compare content with it, you must use 'eq' in this case.
Re: How to compare array with module
by shmem (Chancellor) on Jul 03, 2006 at 09:13 UTC
    The pseudo code you posted gives me the impression that you just whipped it up to have something to post, so we don't complain, and otherwise do the work (and the thinking) for you.
    open ipfile1 Input1 or die"File not found $!\n";
    gives
    Can't locate object method "ipfile1" via package "Input1"
    see open.
    if(ipfile1 == ipfile2){

    not reading the files, but comparing filehandles numerically? You should already have learned by this thread how to open and read files properly.

    Get a clue.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
A reply falls below the community's threshold of quality. You may see it by logging in.