my $quant = $tree_collection{$years}{$trees}{$size} || 0; #### || 0 #### $year_total += $total; #### #!/usr/bin/perl -w use strict; use diagnostics; use Text::ParseWords; use Data::Dumper; my $quantity = (); my $total_quantity = (); my %tree_collection = (); my %tree_sizes = (); while ( my $line = ) { my @fields = quotewords( ',', 0, $line ); my ( $year, $tree, $size, $quantity ) = (@fields)[ 0 .. 4 ]; $total_quantity += $quantity; $tree_collection{$year}{$tree}{$size} += $quantity; } # print Dumper ( \%tree_collection ); print "\n"; for my $years ( sort keys %tree_collection ) { my %sizeOfTrees = (); my @sizeOfTrees = (); printf "\n%17s\n", "Planted in year: $years"; #year print print "=" x 21, "\n"; for my $trees ( sort keys %{ $tree_collection{$years} } ) { for my $size ( sort keys %{ $tree_collection{$years}{$trees} } ) { my @keys = keys %{ $tree_collection{$years}{$trees} }; @sizeOfTrees{@keys} = (); # headers (size) } } @sizeOfTrees = sort keys %sizeOfTrees; printf "%20s:", "size"; for (@sizeOfTrees) { printf " %8s", $_; # headers } printf "%9s", "Totaal\n"; print "\n"; my $total = (); my $year_total = (); for my $trees ( sort keys %{ $tree_collection{$years} } ) { printf "%20s:", $trees; for my $size (@sizeOfTrees) { my $quant = $tree_collection{$years}{$trees}{$size}; # my $quant = $tree_collection{$years}{$trees}{$size} || "-"; printf "%9s", "$quant"; # { no warnings; $total += $quant; } # WHY DO I NEED THIS? } $year_total += $total; printf "%5s", "$total"; $total = 0; print "\n"; } printf "\n%21s", "Totaal:"; my %total_col; for my $i ( keys %{ $tree_collection{$years} } ) { for my $j ( keys %{ $tree_collection{$years}{$i} } ) { $total_col{$j} += $tree_collection{$years}{$i}->{$j}; } } # print Dumper ( \%total_col); for my $i ( sort keys %total_col ) { printf "%9s", "$total_col{$i}"; } printf "%5s", "$year_total"; print "\n"; } print "\n"; # year-tree-size-q print "Total no trees planted: $total_quantity\n"; __DATA__ 2010,Oak,10,5 2010,Oak,20,2 2010,Cypress,20,3 2010,Basswood,24,7 2009,Oak,10,4 2009,Oak,25,2 2009,Cypress,20,1 2008,Basswood,25,9 2011,Pine,11,5