niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » Perl » modules
Text::Diff usage question
Post new topic   Reply to topic Page 1 of 1 [4 Posts] View previous topic :: View next topic
Author Message
Ronald Fischer
*nix forums beginner


Joined: 21 Oct 2005
Posts: 14

PostPosted: Mon Jul 17, 2006 1:15 pm    Post subject: Text::Diff usage question Reply with quote

I would like to compare two arrays of lines. If the arrays
are different, I want to output the differences in human-readable
form, and also call a function f().

I think that Text::Diff, found on CPAN, would useful. Indeed it
works, but my code looks a bit cumbersome and I wonder whether
one can make it easier:

# Compare the arrays
my @status_diff=Text::Diff::diff(\@after,\@before);

unless(@status_diff eq 0
|| (@status_diff eq 1 and length($status_diff[0]) eq 0))
{
# Lines differ
print(@status_diff); # print differences
f() # call f
}

What looks a bit complicated is the condition inside the "unless".
Even on identical arrays, diff returns an array of 1 string of
zero length. But I can't simplify it to, say

if(length($status_diff[0])) { print(...); f() }

because I could imagine that there are cases where diff
returns an empty list or even undef (I didn't find anything
in the diff documentation which guarantees that the output
is always an array of at least one string, when the default
formatter is used - or did I overlook something here in the
docs?).

Any suggestion how I could write this in a better way?

Ronald

--
Ronald Fischer <ronaldf@eml.cc>
Posted via http://www.newsoffice.de/
Back to top
DJ Stunks
*nix forums Guru Wannabe


Joined: 20 Dec 2005
Posts: 192

PostPosted: Wed Jul 19, 2006 4:01 am    Post subject: Re: Text::Diff usage question Reply with quote

Ronald Fischer wrote:
Quote:
I would like to compare two arrays of lines. If the arrays
are different, I want to output the differences in human-readable
form, and also call a function f().

I think that Text::Diff, found on CPAN, would useful. Indeed it
works, but my code looks a bit cumbersome and I wonder whether
one can make it easier:

# Compare the arrays
my @status_diff=Text::Diff::diff(\@after,\@before);

unless(@status_diff eq 0
|| (@status_diff eq 1 and length($status_diff[0]) eq 0))
{
# Lines differ
print(@status_diff); # print differences
f() # call f
}

What looks a bit complicated is the condition inside the "unless".
Even on identical arrays, diff returns an array of 1 string of
zero length. But I can't simplify it to, say

if(length($status_diff[0])) { print(...); f() }

because I could imagine that there are cases where diff
returns an empty list or even undef (I didn't find anything
in the diff documentation which guarantees that the output
is always an array of at least one string, when the default
formatter is used - or did I overlook something here in the
docs?).

I don't know what docs you have, but mine say:

[Synopsis] my $diff = diff \@records1, \@records2;
and
[Output] If no OUTPUT is supplied, returns the diffs in a string

which leads me to believe your array will only ever have one element -
it just may or may not be defined.

-jp
Back to top
ro.naldfi.scher@gmail.com
*nix forums beginner


Joined: 24 Oct 2005
Posts: 28

PostPosted: Wed Jul 19, 2006 8:13 am    Post subject: Re: Text::Diff usage question Reply with quote

DJ Stunks schrieb:
Quote:
# Compare the arrays
my @status_diff=Text::Diff::diff(\@after,\@before);

unless(@status_diff eq 0
|| (@status_diff eq 1 and length($status_diff[0]) eq 0))
{
# Lines differ
print(@status_diff); # print differences
f() # call f
}

What looks a bit complicated is the condition inside the "unless".
Even on identical arrays, diff returns an array of 1 string of
zero length. But I can't simplify it to, say

if(length($status_diff[0])) { print(...); f() }

because I could imagine that there are cases where diff
returns an empty list or even undef (I didn't find anything
in the diff documentation which guarantees that the output
is always an array of at least one string, when the default
formatter is used - or did I overlook something here in the
docs?).

I don't know what docs you have, but mine say:

[Synopsis] my $diff = diff \@records1, \@records2;
and
[Output] If no OUTPUT is supplied, returns the diffs in a string

which leads me to believe your array will only ever have one element -
it just may or may not be defined.

I overlooked this. Thank you for pointing it out.

This makes the comparision a little bit simpler,
although it would be better IMO if the docs ensure exactly how
the result will look like when the arrays are equal (from the code
you can see that the output will be the string "" in such a case,
but this is not the same as documenting this fact - because if
it is documented, one can expect it to be part of the interface
and that it will stay the same in future versions of this module).

Ronald
Back to top
DJ Stunks
*nix forums Guru Wannabe


Joined: 20 Dec 2005
Posts: 192

PostPosted: Wed Jul 19, 2006 7:25 pm    Post subject: Re: Text::Diff usage question Reply with quote

ro.naldfi.scher@gmail.com wrote:
Quote:
DJ Stunks schrieb:
# Compare the arrays
my @status_diff=Text::Diff::diff(\@after,\@before);

unless(@status_diff eq 0
|| (@status_diff eq 1 and length($status_diff[0]) eq 0))
{
# Lines differ
print(@status_diff); # print differences
f() # call f
}

What looks a bit complicated is the condition inside the "unless".
Even on identical arrays, diff returns an array of 1 string of
zero length. But I can't simplify it to, say

if(length($status_diff[0])) { print(...); f() }

because I could imagine that there are cases where diff
returns an empty list or even undef (I didn't find anything
in the diff documentation which guarantees that the output
is always an array of at least one string, when the default
formatter is used - or did I overlook something here in the
docs?).

I don't know what docs you have, but mine say:

[Synopsis] my $diff = diff \@records1, \@records2;
and
[Output] If no OUTPUT is supplied, returns the diffs in a string

which leads me to believe your array will only ever have one element -
it just may or may not be defined.

I overlooked this. Thank you for pointing it out.

This makes the comparision a little bit simpler,
although it would be better IMO if the docs ensure exactly how
the result will look like when the arrays are equal (from the code
you can see that the output will be the string "" in such a case,
but this is not the same as documenting this fact - because if
it is documented, one can expect it to be part of the interface
and that it will stay the same in future versions of this module).

the comparison is much simpler: if ( $diff ne q{} ) { #different...

I agree the module documentation should be updated to include the
return value of diff(). You should contact Barry and let him know.

-jp

Quote:

Ronald
Back to top
Google

Back to top
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [4 Posts] View previous topic :: View next topic
The time now is Thu Nov 20, 2008 10:12 pm | All times are GMT
navigation Forum index » Programming » Perl » modules
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Newbie question: How to forward a domain to a mailbox? leei Postfix 0 Fri Aug 24, 2007 4:55 pm
No new posts configuration question for httpd Karl Wang Apache 1 Fri Jul 21, 2006 2:10 pm
No new posts nim problem/question Ron AIX 0 Fri Jul 21, 2006 1:57 pm
No new posts Oracle Text Score Computation jatinder.1975@gmail.com Server 0 Fri Jul 21, 2006 1:00 pm
No new posts question for JAVA developer who r using postgres sql as b... deepak pal PostgreSQL 1 Fri Jul 21, 2006 9:00 am

Loans | Mortgages | Mortgages | Mortgage Loans | MPAA
Copyright © 2004-2005 DeniX Solutions SRL
 
Other DeniX Solutions sites: Unix/Linux blog |  electronics forum |  medicine forum |  science forum | 
Privacy Policy


Powered by phpBB © 2001, 2005 phpBB Group
[ Time: 0.2505s ][ Queries: 16 (0.1594s) ][ GZIP on - Debug on ]