trim the last blank-line and compare files

Discussion in 'Perl Misc' started by Subhash, Mar 2, 2010.

  1. Subhash

    Subhash Guest

    Hi

    Im comparing two files. Newer file has blank-line introduced at the
    end. So the file difference is only a blank-line at the end. If I need
    to ignore this difference, module File::Compare does not help.

    tried with the third argument to the cmp function:
    cmp("file1","file2", sub { chomp($_[0]) ne chomp($_[1]) })
    which would chomp the lines and then compare the difference if any.
    But still there is difference.

    Can you please let me know where Im making the mistake.

    Thanks
    Subhash
     
    Subhash, Mar 2, 2010
    #1
    1. Advertising

  2. Subhash

    Subhash Guest

    On Mar 2, 5:32 pm, Subhash <> wrote:
    > Hi
    >
    > Im comparing two files. Newer file has blank-line introduced at the
    > end. So the file difference is only a blank-line at the end. If I need
    > to ignore this difference, module File::Compare does not help.
    >
    > tried with the third argument to the cmp function:
    >   cmp("file1","file2", sub { chomp($_[0]) ne chomp($_[1]) })
    > which would chomp the lines and then compare the difference if any.
    > But still there is difference.
    >
    > Can you please let me know where Im making the mistake.
    >
    > Thanks
    > Subhash


    tweaked a bit, added chomp to a subroutine:
    sub trim($) {
    my $line = $_[0];
    chomp($line);
    return $line;
    }
    my $result = cmp("file1","file2", sub {trim $_[0] ne trim $_[1]});

    but still the comparison fails
     
    Subhash, Mar 2, 2010
    #2
    1. Advertising

  3. Subhash

    ccc31807 Guest

    On Mar 2, 7:32 am, Subhash <> wrote:
    > Im comparing two files. Newer file has blank-line introduced at the
    > end. So the file difference is only a blank-line at the end.


    Tie::File lets you treat a file like an array. You might pop the last
    line of the file you are treating like an array. I'll try this later
    on in the day when I get a break and post back.

    CC.
     
    ccc31807, Mar 2, 2010
    #3
  4. Subhash

    Uri Guttman Guest

    >>>>> "S" == Subhash <> writes:

    S> On Mar 2, 5:32 pm, Subhash <> wrote:
    >>
    >> Im comparing two files. Newer file has blank-line introduced at the
    >> end. So the file difference is only a blank-line at the end. If I need
    >> to ignore this difference, module File::Compare does not help.
    >>
    >> tried with the third argument to the cmp function:
    >>   cmp("file1","file2", sub { chomp($_[0]) ne chomp($_[1]) })
    >> which would chomp the lines and then compare the difference if any.
    >> But still there is difference.


    how are you getting the cmp() function into your code? are you
    explicitly importing it? from the module docs:

    File::Compare::cmp is a synonym for File::Compare::compare. It is
    exported from File::Compare only by request.

    use the longer name as you may be getting the builtin cmp op instead.

    S> tweaked a bit, added chomp to a subroutine:
    S> sub trim($) {
    S> my $line = $_[0];
    S> chomp($line);
    S> return $line;
    S> }

    are you sure the blank line only has a newline in it? you need to check/trim
    all trailing white space if it isn't just a newline.

    S> my $result = cmp("file1","file2", sub {trim $_[0] ne trim $_[1]});

    have you run the unix diff command and seen that is the only difference?
    how do you know it is only a trailing blank line that is the failing
    diff?

    uri

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
     
    Uri Guttman, Mar 2, 2010
    #4
  5. Subhash

    Subhash Guest

    On Mar 2, 7:56 pm, "Uri Guttman" <> wrote:
    > >>>>> "S" == Subhash  <> writes:

    >
    >   S> On Mar 2, 5:32 pm, Subhash <> wrote:
    >   >>
    >   >> Im comparing two files. Newer file has blank-line introduced at the
    >   >> end. So the file difference is only a blank-line at the end. If I need
    >   >> to ignore this difference, module File::Compare does not help.
    >   >>
    >   >> tried with the third argument to the cmp function:
    >   >> cmp("file1","file2", sub { chomp($_[0]) ne chomp($_[1]) })
    >   >> which would chomp the lines and then compare the difference if any..
    >   >> But still there is difference.
    >
    > how are you getting the cmp() function into your code? are you
    > explicitly importing it? from the module docs:
    >
    >         File::Compare::cmp is a synonym for File::Compare::compare.  It is
    >         exported from File::Compare only by request.
    >
    > use the longer name as you may be getting the builtin cmp op instead.
    >
    >   S> tweaked a bit, added chomp to a subroutine:
    >   S> sub trim($) {
    >   S>         my $line = $_[0];
    >   S>         chomp($line);
    >   S>         return $line;
    >   S> }
    >
    > are you sure the blank line only has a newline in it? you need to check/trim
    > all trailing white space if it isn't just a newline.
    >
    >   S> my $result = cmp("file1","file2", sub {trim $_[0] ne trim $_[1]});
    >
    > have you run the unix diff command and seen that is the only difference?
    > how do you know it is only a trailing blank line that is the failing
    > diff?
    >
    > uri
    >
    > --
    > Uri Guttman  ------    --------  http://www.sysarch.com--
    > -----  Perl Code Review , Architecture, Development, Training, Support ------
    > ---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com---------


    CC, presently have read the entire file into an array and removing
    blank-lines from the last.

    Yes Uri, have explicitly imported cmp of File::Compare module.
    ran diff under UNIX, it showed the difference in the last line only.
    (ie) the blank line

    Thanks
    Subhash
     
    Subhash, Mar 2, 2010
    #5
  6. Subhash

    Uri Guttman Guest

    >>>>> "S" == Subhash <> writes:

    S> Yes Uri, have explicitly imported cmp of File::Compare module.
    S> ran diff under UNIX, it showed the difference in the last line only.
    S> (ie) the blank line

    but was it just a newline which chomp could handle or were there blanks
    in it? diff won't show you that. you need to look carefully at that last
    line to see what is in it. then write your trim to handle it.

    uri

    --
    Uri Guttman ------ -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
     
    Uri Guttman, Mar 2, 2010
    #6
  7. Subhash <> wrote:
    >Im comparing two files. Newer file has blank-line introduced at the
    >end. So the file difference is only a blank-line at the end.[...]
    >which would chomp the lines and then compare the difference if any.
    >But still there is difference.


    This is a wild guess only but a blank line even after it is chomp()ed is
    still something different than no line (empty string versus EOF or
    undef).

    Even more: if I were to implement this function then the first thing I
    would check is the file size. If the file size is different -and your
    second file must be larger by your own description- then don't even
    bother comparing the content because the contents cannot possibly be the
    same.

    The easiest solution might be to append a blank line to the older file,
    too, if necessary in a temporary copy, and then compare their contents.

    jue
     
    Jürgen Exner, Mar 2, 2010
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. FAQ server
    Replies:
    0
    Views:
    145
    FAQ server
    Aug 29, 2006
  2. FAQ server
    Replies:
    0
    Views:
    142
    FAQ server
    Oct 26, 2006
  3. FAQ server
    Replies:
    6
    Views:
    222
    Jonas Raoni
    Dec 25, 2006
  4. FAQ server
    Replies:
    26
    Views:
    302
    Dr J R Stockton
    Feb 26, 2007
  5. FAQ server
    Replies:
    2
    Views:
    135
    -Lost
    Apr 24, 2007
Loading...

Share This Page