Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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

by jwkrahn (Monsignor)
on Jan 26, 2012 at 16:15 UTC ( #950155=note: print w/replies, xml ) Need Help??


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

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"?

Replies are listed 'Best First'.
Re^2: Creating Reports with hashes: Use of uninitialized value while adding integers
by ikegami (Pope) on Jan 26, 2012 at 18:20 UTC

    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.

Re^2: Creating Reports with hashes: Use of uninitialized value while adding integers
by GertMT (Friar) on Jan 26, 2012 at 16:31 UTC
    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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://950155]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2017-02-24 15:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?






    Results (355 votes). Check out past polls.