Line 61 and 81 in your pasted code are both blank lines. The actual line numbers you are seeing warning messages on are either probably 62, and 82 of what you posted.

"How to get the key - value names from a hash":

my %hash = (foo => 42, bar => 84); # Here is the easiest way: my @kv_pairs = %hash print "All keys and all values as a flattened list: ", @kv_pairs, "\n" +; # Ok, an even easier way: print "All keys and all values as a flattened list: ", %hash, "\n"; # +Because, list context. # Here is another way: while (my ($key, $value) = each %hash) { print "$key => $value\n"; } # Here is another way: my @keys = keys %hash; my @values = values %hash; for my $i (0.. $#keys) { # (fixed) print "$keys[$i] => $values[$i]\n"; } # And yet another: foreach my $key (keys %hash) { print "$key => $hash{$key}\n"; }

The warnings you are seeing on line 62 of the code you posted, and line 82:

On line 82 you reference $average{0}. That means that $avg must, at least one time, have equated to zero. Put a print statement like this: print "avg: $avg\n", "\$avg is exactly 0:", ($avg == 0 ? " yes" : " no"), "\n"; on the line immediately following where $avg gets defined. I'll bet you don't see any exact zeros.

On line 62 you are printing "$odd_even{0}", but nothing ever puts an element names '0' in your %odd_even hash. Why do you think that after creating a hash key that looks like "9 9" you could then expect to be able to print a hash element named 0?

I think that spending 30 minutes reading perlintro and possibly another ten minutes skimming perldata would clear things up. As I've learned other languages I've often wished those languages had such good documentation.


Dave


In reply to Re: How to get the key - value names from a hash by davido
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.