Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Creating Reports with hashes: Use of uninitialized value while adding integers

by GertMT (Friar)
on Jan 26, 2012 at 15:09 UTC ( #950114=perlquestion: print w/ replies, xml ) Need Help??
GertMT has asked for the wisdom of the Perl Monks concerning the following question:

...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

Comment on Creating Reports with hashes: Use of uninitialized value while adding integers
Select or Download Code
Re: Creating Reports with hashes: Use of uninitialized value while adding integers
by JavaFan (Canon) on Jan 26, 2012 at 15:16 UTC
    Though I do not understand why I need to gave "no warnings - line 52" for a column that only has integers.

    ...

    my $quant = $tree_collection{$years}{$trees}{$size} || "-";
    If $tree_collection{$years}{$trees}{$size} is 0, or undefined, $quant is -, which isn't an integer.

    Whether this is happening or not, I cannot be bothered to find out. You actually print the value of $quant, but you're not showing us the output. And I really am not going to download your program and run it myself.

Re: Creating Reports with hashes: Use of uninitialized value while adding integers
by Anonymous Monk on Jan 26, 2012 at 15:17 UTC

    Can you explain, Why did you add it?

    striving for socratic
Re: Creating Reports with hashes: Use of uninitialized value while adding integers
by toolic (Chancellor) on Jan 26, 2012 at 15:23 UTC
    If I change:
    my $quant = $tree_collection{$years}{$trees}{$size} || "-" +;
    to:
    my $quant = $tree_collection{$years}{$trees}{$size} || 0;
    One group of your warning messages goes away:
    Argument "-" isn't numeric in addition (+)
    You must decide if this still gives you the output you need.
Re: Creating Reports with hashes: Use of uninitialized value while adding integers
by ww (Bishop) on Jan 26, 2012 at 15:28 UTC
        ...ummmmm, since it's commented out, maybe you don't need it?

    Please, clean up your question by explaining what happens when line 52 IS commented out, and what happens when it is not commented out. I'm with JavaFan (I think), in that downloading and running code when the question is imprecise takes away from the time available to answer better specified SOPW.

Re: Creating Reports with hashes: Use of uninitialized value while adding integers
by Crackers2 (Vicar) on Jan 26, 2012 at 16:06 UTC

    Shouldn't

    my $quantity = (); my $total_quantity = ();

    be

    my $quantity = 0; my $total_quantity = 0;
Re: Creating Reports with hashes: Use of uninitialized value while adding integers
by jwkrahn (Monsignor) on Jan 26, 2012 at 16:15 UTC

    You need to change:

    for my $size (@sizeOfTrees) { my $quant = $tree_collection{$years}{$trees}{$size} || "-" +; printf "%9s", "$quant"; # { no warnings; $total += $quant; } # WHY DO I NEED TH +IS? }

    To:

    for my $size (@sizeOfTrees) { my $quant = $tree_collection{$years}{$trees}{$size} || 0; printf "%9s", $quant || '-'; $total += $quant; }


    my $quantity = (); my $total_quantity = (); my $total = (); my $year_total = ();

    Why are you trying to assign a list to a scalar variable?    Don't do that, it makes no sense!

    printf "%9s", "$quant"; printf "%5s", "$total"; printf "%9s", "$total_col{$i}"; printf "%5s", "$year_total";

    Don't quote scalar variables.    Perl is not a shell!    What's wrong with always quoting "$vars"?

      Great help and some extra tips.

      I meant to assign the scalars to UNDEF. Now changed, as proposed by others, declaring the scalar variables to 0;

      It looks much cleaner with the NOT quoted scalar variables.

      Thanks again.

        I meant to assign the scalars to UNDEF.

        $foo = (); will indeed do that, but since scalars are initialised to undef on creation, my $foo; is sufficient.

      Why are you trying to assign a list to a scalar variable? Don't do that, it makes no sense!

      It may not be the most readable, but that doesn't meant it makes no sense.

      Not to mention, no list op or list value is involved. Empty parens in scalar context evaluate to undef.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://950114]
Approved by rovf
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (14)
As of 2014-04-23 09:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (541 votes), past polls