Text::Diff usage question

Ask a Question related to PERL Modules, Design and Development.

  1. #1

    Default Text::Diff usage question

    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 [url]http://www.newsoffice.de/[/url]

    Ronald Fischer Guest

  2. Similar Questions and Discussions

    1. Question about CPAN's Text::Diff
      Hi all: This newbie would like to compare two files, let's say File1 and File2. I want to put the difference from File2 only, into a new file,...
    2. One question on ColorSpace usage via SDk
      Hi, If particular element uses ColorSpace - then I see structure like this: /CS0 cs 0 scn .... Is it possible via PDE layer to change that...
    3. Space Usage on Informix Question
      Whats the best way to make sure you dont run out of space on your machines ? I've set up some purge scripts which will purge a lot of old data on...
    4. ASP.NET MSSQL usage question
      The MSDN article about using MSSQL with ASP.NET located here: http://msdn.microsoft.com/library/default.asp? url=/library/en-...
    5. Index Usage Question
      your range scan is forcing the full table scan. AND reportdatentime >= '25-Mar-2003' AND reportdatentime < '21-Jun-2003' Oracle can only...
  3. #2

    Default Re: Text::Diff usage question

    Ronald Fischer wrote:
    > 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

    DJ Stunks Guest

  4. #3

    Default Re: Text::Diff usage question

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

    Ronald

    ro.naldfi.scher@gmail.com Guest

  5. #4

    Default Re: Text::Diff usage question


    [email]ro.naldfi.scher@gmail.com[/email] wrote:
    > 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
    >
    > Ronald
    DJ Stunks Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139