Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Text::Diff::Table output, but want to show all lines

by 2teez (Vicar)
on Dec 12, 2014 at 01:36 UTC ( #1110114=note: print w/replies, xml ) Need Help??


in reply to Text::Diff::Table output, but want to show all lines

Hi Solo,
..But sometimes I want to show all the lines, not only the differences with surrounding context..

I don't know how you are using Text::Diff::Table and what you mean to show all the lines.
For in my experience, I have a table of all the lines. And the lines with difference are indicated by * at both side of the table, showing both files.
More like what diff command in Linux will give you when used with the option y like so: diff -y file1 file2

Update:

If you do this, in your script
my $diff = diff $file1, $file2, { STYLE => "Table" }; print $diff;
You will have a table, comparing the two files, and showing differences. Just like I mentioned previously.

But without the { STYLE => "Table" } in that script, you will only have want I suppose you have now.
And like I said, you weren't telling how you are using the module. But, I think this is what you want.

Please Note:
That if you use diff $file1, $file2 { STYLE => "Table" }; as given from the documentation of Text::Diff::Table, which is a bit different from that of Text::Diff. Note there is no , after $file2, before {. And that without the use strict; pragama, you have just the difference between the files listed and no error BUT with the use strict;, you will end up with compilation errors.

If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me

Replies are listed 'Best First'.
Re^2: Text::Diff::Table output, but want to show all lines
by Solo (Deacon) on Dec 12, 2014 at 19:40 UTC
    Thanks for the reply, 2teez! Apologies that I wasn't clear enough. Say I have the one-liner, which is not what I'm really trying to do and only to demonstrate the behaviors,
    perl -MText::Diff -e '$size = 10; @a = sort map { int rand(100) } 1..$ +size; @b = sort map { int rand(100) } 1..$size; print diff \@a, \@b, +{ STYLE => "Table" };'
    I don't quite understand how it's supposed to work without the comma after the 2nd item to compare. If you try that in the one-liner, you'll get the error Not a HASH reference at /usr/local/share/perl/5.18.2/Text/Diff.pm line 46. Pressing on anyway, if I run the one-liner with small size which yields many differences and few matching chunks, I do see all the lines as I want to.
    +--+----+--+----+
    * 0|12  * 0|0   *
    * 1|2   * 1|42  *
    * 2|24  * 2|43  *
    * 3|26  *  |    |
    * 4|29  *  |    |
    * 5|3   *  |    |
    * 6|4   *  |    |
    * 7|48  *  |    |
    | 8|49  | 3|49  |
    * 9|63  * 4|5   *
    |  |    * 5|59  *
    |  |    * 6|6   *
    |  |    * 7|77  *
    |  |    * 8|9   *
    |  |    * 9|95  *
    +--+----+--+----+
    
    However, when you start running with $size=1000 or $size=10000, you'll see breaks in the output where there are large chunks of identical entries in the lists and only the differences and surrounding context is shown. For example,
    ...
    |     |    * 8127|81  *
    |     |    * 8128|81  *
    | 8157|82  | 8129|82  |
    | 8158|82  | 8130|82  |
    | 8159|82  | 8131|82  |
    +-----+----+-----+----+  <--- lots of identical entries hidden in that break
    | 8255|82  | 8227|82  |
    | 8256|82  | 8228|82  |
    | 8257|82  | 8229|82  |
    * 8258|82  *     |    |
    * 8259|82  *     |    |
    ...
    
    For the normal use of diff, hiding the large identical chunks and showing only the differences with context is desirable (arguably, it's the whole point!) But for what I'm working on now, I want to show all of the roughly 6000 lines even if large chunks are identical. I'm hoping there's a way of forcing that behavior or of controlling whatever the chunk break setting is. :)
      I have the same problem. According to some documentation I found,the Context default causes a window of 3 lines around differences to be shown. So need to increase the Window size. Haven't been able to find how to do so. Does anyone out there know?
        #!/usr/bin/perl use strict; use warnings; use Text::Diff; use List::Util qw( max ); my @one = <<END =~ /.*\n/g; one two three four five six seven eight nine ten END my @two = <<END =~ /.*\n/g; one twoplus three four five six seven eight nine tenminus END print "no CONTEXT specified:\n"; print diff \@one, \@two, {STYLE => 'Table' }; print "\nlarge CONTEXT specified:\n"; print diff \@one, \@two, {STYLE => 'Table', CONTEXT => max( $#one, $#t +wo) };

        Outputs:

        no CONTEXT specified: +--+-------+----------+ | 0|one |one | * 1|two |twoplus * | 2|three |three | | 3|four |four | | 4|five |five | +--+-------+----------+ | 6|seven |seven | | 7|eight |eight | | 8|nine |nine | * 9|ten |tenminus * +--+-------+----------+ large CONTEXT specified: +--+-------+----------+ | 0|one |one | * 1|two |twoplus * | 2|three |three | | 3|four |four | | 4|five |five | | 5|six |six | | 6|seven |seven | | 7|eight |eight | | 8|nine |nine | * 9|ten |tenminus * +--+-------+----------+

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1110114]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2021-09-17 17:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?