Formating output to window

S

shai.ovadia

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

Anno Siegel

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
 
T

Tad McClellan

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
 
S

Shai

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

Shai.
 
J

John W. Krahn

Tad said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,519
Latest member
OtisLucket

Latest Threads

Top