M
Mothra
Hi All,
I have a data structure (see script) that I need to printout. I almost have
what I
need but I cannot figure out how to format just one line (center it )
sample output is:
kreimer
EMMI CHRISTIAN 0 0 17
TRACEY LUSBY 0 4 0
TONA MCGEAN 0 7 0
Total: 0 11 17
What I would like is the word "kreimer" centered and the other names
remain left justified. What am I doing wrong?
Thanks
Lynn
----------script--------------
use strict;
use warnings;
use Text::Table;
use diagnostics;
use Data:
umper;
my $people = {
'kreimer' => {
'TRACEY LUSBY' => [ 0, 4, 0 ],
'EMMI CHRISTIAN' => [ 0, 0, 17 ],
'TONA MCGEAN' => [ 0, 7, 0 ],
},
'OTHER' => {
'STEPHEN ESPESET' => [ undef, 4, 27 ],
'PATRICK HOONHOU' => [ 1, 3, 4 ],
'WOODROW WILSON' => [ undef, 2, 1 ],
},
'lusby' => {
'KEVIN FLUETSCH' => [ 0, 0, 22 ],
'DOUG BOSWORTH' => [ 0, 1, 6 ],
'DOUG PENICK' => [ 1, 2, 22 ],
},
};
foreach my $tmp_people( values %{ $people->{'OTHER'} } ) {
if ( !defined( $tmp_people->[0] ) ) { $tmp_people->[0] = 0 }
if ( !defined( $tmp_people->[1] ) ) { $tmp_people->[1] = 0 }
if ( !defined( $tmp_people->[2] ) ) { $tmp_people->[2] = 0 }
}
my %manager_total = ();
foreach my $managers( keys %{$people} ) {
foreach my $person( keys %{ $people->{$managers} } ) {
$manager_total{$managers}[0] += $people->{$managers}->{$person}[0];
$manager_total{$managers}[1] += $people->{$managers}->{$person}[1];
$manager_total{$managers}[2] += $people->{$managers}->{$person}[2];
}
}
my $tb = Text::Table->new(
{
title => 'Name',
align => 'left',
align_title => 'center'
},
{
title => 'Last Month',
align => 'center',
align_title => 'center'
},
{
title => 'YTD',
align => 'center',
align_title => 'center'
},
{
title => 'Rolling 12 months',
align => 'center',
align_title => 'center'
},
);
#print Dumper (\%manager_total);
foreach my $managers( keys %{$people} ) {
$tb->add($managers);
foreach my $person( sort Mysort keys %{ $people->{$managers} } ) {
$tb->add( "$person", @{ $people->{$managers}->{$person} } );
}
$tb->add( "Total: ", @{ $manager_total{$managers} } );
$tb->add();
}
print $tb;
sub Mysort {
my ( undef, $first ) = split ( / /, $a );
my ( undef, $last ) = split ( / /, $b );
$first cmp $last;
}
I have a data structure (see script) that I need to printout. I almost have
what I
need but I cannot figure out how to format just one line (center it )
sample output is:
kreimer
EMMI CHRISTIAN 0 0 17
TRACEY LUSBY 0 4 0
TONA MCGEAN 0 7 0
Total: 0 11 17
What I would like is the word "kreimer" centered and the other names
remain left justified. What am I doing wrong?
Thanks
Lynn
----------script--------------
use strict;
use warnings;
use Text::Table;
use diagnostics;
use Data:
my $people = {
'kreimer' => {
'TRACEY LUSBY' => [ 0, 4, 0 ],
'EMMI CHRISTIAN' => [ 0, 0, 17 ],
'TONA MCGEAN' => [ 0, 7, 0 ],
},
'OTHER' => {
'STEPHEN ESPESET' => [ undef, 4, 27 ],
'PATRICK HOONHOU' => [ 1, 3, 4 ],
'WOODROW WILSON' => [ undef, 2, 1 ],
},
'lusby' => {
'KEVIN FLUETSCH' => [ 0, 0, 22 ],
'DOUG BOSWORTH' => [ 0, 1, 6 ],
'DOUG PENICK' => [ 1, 2, 22 ],
},
};
foreach my $tmp_people( values %{ $people->{'OTHER'} } ) {
if ( !defined( $tmp_people->[0] ) ) { $tmp_people->[0] = 0 }
if ( !defined( $tmp_people->[1] ) ) { $tmp_people->[1] = 0 }
if ( !defined( $tmp_people->[2] ) ) { $tmp_people->[2] = 0 }
}
my %manager_total = ();
foreach my $managers( keys %{$people} ) {
foreach my $person( keys %{ $people->{$managers} } ) {
$manager_total{$managers}[0] += $people->{$managers}->{$person}[0];
$manager_total{$managers}[1] += $people->{$managers}->{$person}[1];
$manager_total{$managers}[2] += $people->{$managers}->{$person}[2];
}
}
my $tb = Text::Table->new(
{
title => 'Name',
align => 'left',
align_title => 'center'
},
{
title => 'Last Month',
align => 'center',
align_title => 'center'
},
{
title => 'YTD',
align => 'center',
align_title => 'center'
},
{
title => 'Rolling 12 months',
align => 'center',
align_title => 'center'
},
);
#print Dumper (\%manager_total);
foreach my $managers( keys %{$people} ) {
$tb->add($managers);
foreach my $person( sort Mysort keys %{ $people->{$managers} } ) {
$tb->add( "$person", @{ $people->{$managers}->{$person} } );
}
$tb->add( "Total: ", @{ $manager_total{$managers} } );
$tb->add();
}
print $tb;
sub Mysort {
my ( undef, $first ) = split ( / /, $a );
my ( undef, $last ) = split ( / /, $b );
$first cmp $last;
}