Formating output to window

Discussion in 'Perl Misc' started by shai.ovadia@gmail.com, Mar 8, 2006.

  1. Guest

    Hi all,

    I have a simple text file which has several lines with one word in each
    line.
    All I wanna do is display this file to the screen when the output is
    divided to 4 columns with numbers near each word.

    Let's say the text file is this:
    aa
    bbbbb
    c
    ddd
    eee
    f
    gg
    hhhhhhh
    ....

    I want the output to the screen will look like that:
    1. aa 2. bbbbb 3. c 4. ddd
    5. eee 6. f 7. gg 8. hhhhhhh
    ....

    Where the width between the words (and numbers) must be equal.
    Of course, words length is vary (Some of them can be double size than
    others.)
    Any sugestions please???

    Thanks,
    Shai.
    , Mar 8, 2006
    #1
    1. Advertising

  2. Anno Siegel Guest

    <> wrote in comp.lang.perl.misc:
    > Hi all,
    >
    > I have a simple text file which has several lines with one word in each
    > line.
    > All I wanna do is display this file to the screen when the output is
    > divided to 4 columns with numbers near each word.
    >
    > Let's say the text file is this:
    > aa
    > bbbbb
    > c
    > ddd
    > eee
    > f
    > gg
    > hhhhhhh
    > ...
    >
    > I want the output to the screen will look like that:
    > 1. aa 2. bbbbb 3. c 4. ddd
    > 5. eee 6. f 7. gg 8. hhhhhhh
    > ...
    >
    > Where the width between the words (and numbers) must be equal.
    > Of course, words length is vary (Some of them can be double size than
    > others.)
    > Any sugestions please???


    That's a case for Text::Table, available from CPAN. Here is how:

    use Text::Table;

    # Number of columns
    use constant NCOLS => 4;

    # Generate data (alternating number and word)
    my $i = 0;
    my @data = map { ++ $i . '.', $_ } <DATA>;

    # Create a table with 4 pairs of columns. Separate pairs by
    # three blanks.
    my $tb = Text::Table->new( ( '', '', \ ' ') x NCOLS);

    # Add data in groups of 8
    $tb->add( splice @data, 0, 2*NCOLS) while @data;

    # print result
    print $tb;

    __DATA__
    aa
    bbbbb
    c
    ddd
    eee
    f
    gg
    hhhhhhh

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
    Anno Siegel, Mar 8, 2006
    #2
    1. Advertising

  3. <> wrote:

    > Let's say the text file is this:
    > aa
    > bbbbb
    > c
    > ddd
    > eee
    > f
    > gg
    > hhhhhhh
    > ...
    >
    > I want the output to the screen will look like that:
    > 1. aa 2. bbbbb 3. c 4. ddd
    > 5. eee 6. f 7. gg 8. hhhhhhh



    -------------------------------
    #!/usr/bin/perl
    use warnings;
    use strict;

    chomp(my @words = <DATA>);

    my $cols = 4;
    my $len = max( @words ) + 2; # at least 2 spaces

    foreach my $i ( 1 .. @words ) {
    printf "%2d. %-${len}s", $i, $words[$i-1]; # a single column
    print "\n" if $i % $cols == 0; # time for a newline
    }

    sub max {
    my($max) = length shift;
    foreach ( @_ ) {
    my $len = length;
    $max = $len if $len > $max;
    }
    return $max;
    }

    __DATA__
    aa
    bbbbb
    c
    ddd
    eee
    f
    gg
    hhhhhhh
    -------------------------------


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Mar 8, 2006
    #3
  4. Shai Guest

    I actually used the second option but I wanna
    thank you both for your great and useful help!!!!!!!!!!!

    Shai.
    Shai, Mar 8, 2006
    #4
  5. Tad McClellan wrote:
    > <> wrote:
    >
    >>Let's say the text file is this:
    >>aa
    >>bbbbb
    >>c
    >>ddd
    >>eee
    >>f
    >>gg
    >>hhhhhhh
    >>...
    >>
    >>I want the output to the screen will look like that:
    >>1. aa 2. bbbbb 3. c 4. ddd
    >>5. eee 6. f 7. gg 8. hhhhhhh

    >
    >
    > -------------------------------
    > #!/usr/bin/perl
    > use warnings;
    > use strict;
    >
    > chomp(my @words = <DATA>);
    >
    > my $cols = 4;
    > my $len = max( @words ) + 2; # at least 2 spaces
    >
    > foreach my $i ( 1 .. @words ) {
    > printf "%2d. %-${len}s", $i, $words[$i-1]; # a single column


    Don't you mean:

    printf '%2d. %-*s', $i, $len, $words[$i-1]; # a single column

    :)



    John
    --
    use Perl;
    program
    fulfillment
    John W. Krahn, Mar 9, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. John Bentley

    Output / Debug window output bug?

    John Bentley, Sep 10, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    577
    John Bentley
    Sep 10, 2003
  2. Johannes Barop

    Formating Output of std::ostream

    Johannes Barop, Dec 26, 2005, in forum: C++
    Replies:
    10
    Views:
    604
    Dietmar Kuehl
    Dec 27, 2005
  3. ram
    Replies:
    0
    Views:
    288
  4. ram
    Replies:
    1
    Views:
    344
    Andrew Thompson
    Oct 9, 2007
  5. Replies:
    1
    Views:
    144
    Craig Kim
    May 23, 2006
Loading...

Share This Page