Dan wrote:
>
> ok, this probably has an easy answer, but yet i can't think of it. i want
> to create a "table", not a table in html, otherwise this would be so easy,
> but it's not. the idea is, i have variable length data, and i want to be
> able to create a table out of it, if i give you a sample, and show how i'd
> want perl to output it, any ideas?
>
> sample data:
> 1 Erazor 100 0 0
> 2 AnGeL` 500 0 0
> 4 ^ML^ 100 0 0
> 5 Joe 100 0 1
> 6 Liquid_Snake_911 100 0 0
> 121 Psycho 100 0 0
>
> and i'd want the data to be outputted in this format (it'll be displayed
> using fixed width text):
>
> --------------------------------------------------
> | ID | Nickname | ULEVEL | SLEVEL | AOP |
> |-----|------------------|--------|--------|-----|
> | 1 | Erazor | 100 | 0 | 0 |
> | 2 | AnGeL` | 500 | 0 | 0 |
> | 4 | ^ML^ | 100 | 0 | 0 |
> | 5 | Joe | 100 | 0 | 1 |
> | 6 | Liquid_Snake_911 | 100 | 0 | 0 |
> | 121 | Psycho | 100 | 0 | 0 |
> --------------------------------------------------
>
> but for example, if "Liquid_Snake_911" wasn't there, the table would look like:
>
> ------------------------------------------
> | ID | Nickname | ULEVEL | SLEVEL | AOP |
> |-----|----------|--------|--------|-----|
> | 1 | Erazor | 100 | 0 | 0 |
> | 2 | AnGeL` | 500 | 0 | 0 |
> | 4 | ^ML^ | 100 | 0 | 0 |
> | 5 | Joe | 100 | 0 | 1 |
> | 121 | Psycho | 100 | 0 | 0 |
> ------------------------------------------
>
> so the column is either (length of the title + 2) or
> (length of largest record + 2).
>
> any clues on how this may be acheived?
Here is one way to do it:
#!/usr/bin/perl -w
use strict;
my @header = qw/ID Nickname ULEVEL SLEVEL AOP/;
my @widths = map length, @header;
my @data;
while ( <DATA> ) {
push @data, [ split ];
for ( 0 .. $#{$data[-1]} ) {
$widths[$_] = length $data[-1][$_]
if $widths[$_] < length $data[-1][$_];
}
}
my $total = do { my $t; $t += $_ for @widths; $t };
my $format = join( '', map( "| %-${_}s ", @widths ) ) . '|';
my $line = '-' x ( $total + @widths * 3 + 1 );
print "$line\n";
printf "$format\n", @header;
print "$line\n";
printf "$format\n", @$_ for @data;
print "$line\n";
__DATA__
1 Erazor 100 0 0
2 AnGeL` 500 0 0
4 ^ML^ 100 0 0
5 Joe 100 0 1
6 Liquid_Snake_911 100 0 0
121 Psycho 100 0 0
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]