trim the last blank-line and compare files

S

Subhash

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
 
S

Subhash

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
 
C

ccc31807

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.
 
U

Uri Guttman

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
 
S

Subhash

  >>
  >> 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

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
 
U

Uri Guttman

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
 
J

Jürgen Exner

Subhash said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,708
Latest member
SherleneF1

Latest Threads

Top