Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
...UPDATED...

hello all,

I've been creating various reports with this script I wrote. Though I do not understand why I need to gave "no warnings - line 52" for a column that only has integers. Can someone explain me why this happens?

Data either in DATA or FILE didn't make a difference.

I tried to take out

my $quant = $tree_collection{$years}{$trees}{$size} || 0;
or just
|| 0
However then I get a complain about:
$year_total += $total;
Use of uninitialized value $total in addition (+) at
	/Volumes/gertmt/Documents/Programming-Folder/HoH_TreeCollection.pl line 54, <DATA> line 9 (#1)
...
Use of uninitialized value $total in string at
	/Volumes/gertmt/Documents/Programming-Folder/HoH_TreeCollection.pl line 55, <DATA> line 9 (#1)
Use of uninitialized value $quant in string at
	/Volumes/gertmt/Documents/Programming-Folder/HoH_TreeCollection.pl line 50, <DATA> line 9 (#1)

The output is printed at the bottom of the code.

Thanks for your wisdom,

Gert

#!/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 = <DATA> ) { 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 TH +IS? } $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
Gives with warnings as mentioned above while no value for undefined is given (is the undefined the problem?)
Planted in year: 2008
=====================
                size:       25  Totaal

            Basswood:        9     

              Totaal:        9    0

Planted in year: 2009
=====================
                size:       10       20       25  Totaal

             Cypress:                 1              
                 Oak:        4                 2    0

              Totaal:        4        1        2    0

Planted in year: 2010
=====================
                size:       10       20       24  Totaal

            Basswood:                          7     
             Cypress:                 3             0
                 Oak:        5        2             0

              Totaal:        5        5        7    0

Planted in year: 2011
=====================
                size:       11  Totaal

                Pine:        5     

              Totaal:        5    0

Total no trees planted: 38

In reply to Creating Reports with hashes: Use of uninitialized value while adding integers by GertMT

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others having an uproarious good time at the Monastery: (11)
    As of 2014-09-19 18:56 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (144 votes), past polls