02:17 02:40 02:46 02:59 Apples 0 0 0 0 Grapes 7 7 7 7 bricks 0 0 0 0 TUBELIGHS 282 271 265 257 TOTAL 289 278 272 264 #### #!/usr/bin/perl -w # Strict use strict; use warnings; # Assign variables my $plabels = [ '02:17', '02:40', '02:46', '02:59' ]; my $pcounts = { 'Apples' => [ 0, 0, 0, 0, ], 'Grapes' => [ 7, 7, 7, 7, ], 'bricks' => [ 0, 0, 0, 0, ], 'TUBELIGHS' => [ 282, 271, 265, 257, ], }; my $p_ordered = [ qw( Apples Grapes bricks TUBELIGHS ) ]; my $ncols = 4; # Or $ncols = scalar @{$pcounts->{$p_ordered->[0]}}; # Calculate totals my $pTOTAL = [ ]; foreach my $key (keys %$pcounts) { map { $pTOTAL->[$_] += $pcounts->{$key}->[$_] } (0..$ncols-1); } # Main program print_row("", $plabels, " %7.7s"); map { print_row($_, $pcounts->{$_}, " %7d") } @$p_ordered; print "\n"; print_row("TOTAL", $pTOTAL, " %7d"); # Subroutines sub print_row { my ($label, $pvalues, $format) = @_; printf " %-12.12s", $label; map { printf $format, $_ } @$pvalues; print "\n"; }