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

If I remove a element such as \@percentage, I can print. But this is not my expectation. I want to show many rows, columns.

A diff generally only works between two (or three) files, so perhaps you could explain more about what you want your output to look like? Please show some short, representative sample input, and the expected output for that input, each within <code> tags. See also How do I post a question effectively? and Short, Self-Contained, Correct Example.

Also, a tip: Instead of including all the code to read from the input, it's much easier for us to test by including the input values in the code, e.g. my @codecs = ("0\n", "0\n"); ..., and your sample code will be shorter.

Replies are listed 'Best First'.
Re^3: Text::Diff::Table output, but want to show all lines
by tctoa (Novice) on Jul 05, 2019 at 08:22 UTC
    Hi haukex, Thanks for your response. I want to write a perl scirpt with expectation below: - Input values from keyboard - Output those values in a tables (3 rows, 4 columns ) using Text:Diff:Table This is my code:

    Input values from keyboard

    $a = <STDIN>; my ( @codecs, @channelCap, @percentage ); ## declare codecs variabl +e for ( my $i = 0 ; $i < $a ; $i++ ) { print( "Codec " . ( $i + 1 ) . " = " ); $tmp = <STDIN>; push @codecs, $tmp; print( "Channel capacity of codec " . ( $i + 1 ) . " = " ); $tmp = <STDIN>; push @channelCap, $tmp; print( "Percentage of codec " . ( $i + 1 ) . " = " ); $tmp = <STDIN>; push @percentage, $tmp; }
    Assuming i = 3 There're 3 parameters need to input: type of codecs @codecs = (g711, g722, g729); @channelCap = (1000, 2000, 3000) ; @percentage = (0.5, 0.3, 0.2)

    Output 4 columns: No. | Codec | ChannelCap | Percentage. Each parameter has corresponding values (here is 3). This is the number of row need to show.

    print "No. Codec ChannelCap Percentage\n"; print diff \@codecs, \@channelCap, \@percentage, { STYLE => 'Table' };
    Actually, I only get output bellow:
    Please input the number of codec that using 3 Codec 1 = g711 Channel capacity of codec 1 = 1000 Percentage of codec 1 = 0.5 Codec 2 = g722 Channel capacity of codec 2 = 2000 Percentage of codec 2 = 0.3 Codec 3 = g729 Channel capacity of codec 3 = 3000 Percentage of codec 3 = 0.2 No. Codec ChannelCap Percentage Not a HASH reference at C:/Strawberry/perl/vendor/lib/Text/Diff.pm lin +e 46, <STDIN> line 10.

      Thanks for the additional information, but you didn't say what the expected output for that input is, and I still don't understand what you want to use Text::Diff::Table for - what are you diffing? Are you just trying to show things in a table? There are plenty of such modules available on CPAN, here's Text::Table::Tiny:

      use warnings; use strict; use Text::Table::Tiny 'generate_table'; my @codecs = ('g711', 'g722', 'g729'); my @channelCap = (1000, 2000, 3000); my @percentage = (0.5, 0.3, 0.2); my @table = ( ["No.", "Codec", "ChannelCap", "Percentage"] ); for my $i ( 0 .. $#codecs ) { push @table, [ $i+1, $codecs[$i], $channelCap[$i], $percentage[$i] ]; } print generate_table(rows => \@table, header_row => 1), "\n"; __END__ +-----+-------+------------+------------+ | No. | Codec | ChannelCap | Percentage | +-----+-------+------------+------------+ | 1 | g711 | 1000 | 0.5 | | 2 | g722 | 2000 | 0.3 | | 3 | g729 | 3000 | 0.2 | +-----+-------+------------+------------+
        Hi haukex, Many thanks for your support. I can get expected result following your code...
        +-----+-------+------------+------------+ | No. | Codec | ChannelCap | Percentage | +-----+-------+------------+------------+ | 1 | g711 | 0 | 0 | | 2 | g722 | 0 | 0 | | 3 | g729 | 0 | 0 | +-----+-------+------------+------------+