http://qs321.pair.com?node_id=11109460


in reply to How to get the key - value names from a hash

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