To help figure things out, I very strongly recommend you clean up your code - you've got some stray indentation, a whole bunch of unused variables and ones irrelevant to the question (Update: and lots of whitespace), and in a few cases the order of statements is a bit confusing (e.g. defining $odd and $even in between the use of $column_count). Also, lines 61 and 81 are not producing the warning, as you said. Fixing these things will help people who are trying to help you.

Second, see the Basic debugging checklist, in this case the fourth point: use Data::Dumper or Data::Dump to look at your data structures. I've taken the liberty of doing all the above for you:

#!/usr/bin/env perl use strict; use warnings; use Data::Dump; my @items = (2,14,7,17,1,12,3,4,11,5,13,6,8,10,9,16,15,18); @items = map { sprintf '%02u', $_ } @items; dd @items; @items = sort @items; dd @items; my ($odd,$even)=(0,0); $_ % 2 ? $odd++ : $even++ for @items; print "Odd = $odd, Even = $even\n"; my %odd_even; $odd_even{"$odd $even"}++; dd \%odd_even; # { "9 9" => 1 } dd $odd_even{0}; # undef my $sum; $sum += $_ for @items; my $avg = $sum / @items; $avg = ($avg - int($avg)) >= 0.5 ? int($avg) + 1 : int($avg); my %average; $average{$avg}++; dd \%average; # { 10 => 1 } dd $average{0}; # undef

So the reason you're getting that warning is because each of your hashes have only one key, each of which is not "0". If you want to iterate over a hash, see keys, values, and each. Personally, my code for iterating over a hash typically looks something like this:

for my $key (sort keys %hash) { my $value = $hash{$key}; }

Note that while your method of doing a numeric sort may work in this case, it will break on 3-digit and longer numbers. A numeric sort in Perl is written as: sort {$a<=>$b} @array.

Also, a nitpick: using map only for its side effects can be a little confusing, so I've changed it to a for loop instead.

Update 2: Note that ($avg - int($avg)) >= 0.5 ? int($avg) + 1 : int($avg) can also be written as: use POSIX 'lround'; $avg = lround($avg);


In reply to Re: How to get the key - value names from a hash by haukex
in thread How to get the key - value names from a hash by Scotmonk

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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.