L
Lynn
Hi All,
I am having problems generating a report (Columns) from data
that is in the form of associative arrays of associative arrays.
I have provided an example of what I am trying to do. I the problem
is I am unable to align the percent column. Any advise as
how to fix this?
Thanks
Lynn
#!/app/bin/perl
use warnings;
use strict;
use diagnostics;
use Text::Table;
my %people = (
'hr' => {
'TOTAL' => 15,
'WIN' => 11,
'HP' => 4,
'BLOG' => 2
},
'jg' => {
'TOTAL' => 14,
'WIN' => 5,
'SGI' => 9,
'BLOG' => 4
},
'my' => {
'TOTAL' => 10,
'WIN' => 9,
'BLOG' => 1,
'HP' => 1
},
'gh' => {
'TOTAL' => 13,
'WIN' => 8,
'SUN' => 5,
'BLOG' => 1
}
);
use vars qw(@by_family $TOTAL $BLOG $HP $IBM $SGI $SUN $WIN $OTH);
my @stuff = qw(TOTAL BLOG HP IBM SGI SUN WIN OTH);
while ( my ( $item, $record ) = each %people ) {
$TOTAL += $record->{TOTAL} unless !( defined( $record->{TOTAL} ) );
$OTH += $record->{OTH} unless !( defined( $record->{OTH} ) );
$BLOG += $record->{BLOG} unless !( defined( $record->{BLOG} ) );
$HP += $record->{HP} unless !( defined( $record->{HP} ) );
$IBM += $record->{IBM} unless !( defined( $record->{IBM} ) );
$SGI += $record->{SGI} unless !( defined( $record->{SGI} ) );
$SUN += $record->{SUN} unless !( defined( $record->{SUN} ) );
$WIN += $record->{WIN} unless !( defined( $record->{WIN} ) );
}
push ( @by_family, $TOTAL, $BLOG, $HP, $IBM, $SGI, $SUN, $WIN, $OTH );
my @percent = ();
push ( @percent, sprintf( '%2d', ( 100 * $by_family[2] / $by_family[0] ) ) )
unless !( defined( $by_family[2] ) or '0' );
push ( @percent, sprintf( '%2d', ( 100 * $by_family[3] / $by_family[0] ) ) )
unless !( defined( $by_family[3] ) or '0');
push ( @percent, sprintf( '%2d', ( 100 * $by_family[4] / $by_family[0] ) ) )
unless !( defined( $by_family[4] ) or '0');
push ( @percent, sprintf( '%2d', ( 100 * $by_family[5] / $by_family[0] ) ) )
unless !( defined( $by_family[5] ) or '0');
push ( @percent, sprintf( '%2d', ( 100 * $by_family[6] / $by_family[0] ) ) )
unless !( defined( $by_family[6] ) or '0');
push ( @percent, sprintf( '%2d', ( 100 * $by_family[7] / $by_family[0] ) ) )
unless !( defined( $by_family[7] ) or '0');
my $tb = Text::Table->new( \"|", 'Name', \"|", @stuff );
$tb->add( $_, @{ $people{$_} }{@stuff} ) for sort keys
%people;
$tb->add( 'TOTAL:', @by_family );
$tb->add( 'PERCENT TOTAL:','N/A', 'N/A', @percent );
print $tb;
I am having problems generating a report (Columns) from data
that is in the form of associative arrays of associative arrays.
I have provided an example of what I am trying to do. I the problem
is I am unable to align the percent column. Any advise as
how to fix this?
Thanks
Lynn
#!/app/bin/perl
use warnings;
use strict;
use diagnostics;
use Text::Table;
my %people = (
'hr' => {
'TOTAL' => 15,
'WIN' => 11,
'HP' => 4,
'BLOG' => 2
},
'jg' => {
'TOTAL' => 14,
'WIN' => 5,
'SGI' => 9,
'BLOG' => 4
},
'my' => {
'TOTAL' => 10,
'WIN' => 9,
'BLOG' => 1,
'HP' => 1
},
'gh' => {
'TOTAL' => 13,
'WIN' => 8,
'SUN' => 5,
'BLOG' => 1
}
);
use vars qw(@by_family $TOTAL $BLOG $HP $IBM $SGI $SUN $WIN $OTH);
my @stuff = qw(TOTAL BLOG HP IBM SGI SUN WIN OTH);
while ( my ( $item, $record ) = each %people ) {
$TOTAL += $record->{TOTAL} unless !( defined( $record->{TOTAL} ) );
$OTH += $record->{OTH} unless !( defined( $record->{OTH} ) );
$BLOG += $record->{BLOG} unless !( defined( $record->{BLOG} ) );
$HP += $record->{HP} unless !( defined( $record->{HP} ) );
$IBM += $record->{IBM} unless !( defined( $record->{IBM} ) );
$SGI += $record->{SGI} unless !( defined( $record->{SGI} ) );
$SUN += $record->{SUN} unless !( defined( $record->{SUN} ) );
$WIN += $record->{WIN} unless !( defined( $record->{WIN} ) );
}
push ( @by_family, $TOTAL, $BLOG, $HP, $IBM, $SGI, $SUN, $WIN, $OTH );
my @percent = ();
push ( @percent, sprintf( '%2d', ( 100 * $by_family[2] / $by_family[0] ) ) )
unless !( defined( $by_family[2] ) or '0' );
push ( @percent, sprintf( '%2d', ( 100 * $by_family[3] / $by_family[0] ) ) )
unless !( defined( $by_family[3] ) or '0');
push ( @percent, sprintf( '%2d', ( 100 * $by_family[4] / $by_family[0] ) ) )
unless !( defined( $by_family[4] ) or '0');
push ( @percent, sprintf( '%2d', ( 100 * $by_family[5] / $by_family[0] ) ) )
unless !( defined( $by_family[5] ) or '0');
push ( @percent, sprintf( '%2d', ( 100 * $by_family[6] / $by_family[0] ) ) )
unless !( defined( $by_family[6] ) or '0');
push ( @percent, sprintf( '%2d', ( 100 * $by_family[7] / $by_family[0] ) ) )
unless !( defined( $by_family[7] ) or '0');
my $tb = Text::Table->new( \"|", 'Name', \"|", @stuff );
$tb->add( $_, @{ $people{$_} }{@stuff} ) for sort keys
%people;
$tb->add( 'TOTAL:', @by_family );
$tb->add( 'PERCENT TOTAL:','N/A', 'N/A', @percent );
print $tb;