note
davido
<p>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.</p>
<p>"How to get the key - value names from a hash":
</p>
<c>
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";
}
</c>
<p>The warnings you are seeing on line 62 of the code you posted, and line 82:</p>
<p>On line 82 you reference <c>$average{0}</c>. That means that <c>$avg</c> must, at least one time, have equated to zero. Put a print statement like this: <c>print "avg: $avg\n", "\$avg is exactly 0:", ($avg == 0 ? " yes" : " no"), "\n";</c> on the line immediately following where <c>$avg</c> gets defined. I'll bet you don't see any exact zeros.</p>
<p>On line 62 you are printing <c>"$odd_even{0}"</c>, but nothing ever puts an element names '0' in your <c>%odd_even</c> 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?</p>
<p>I think that spending 30 minutes reading [doc://perlintro] and possibly another ten minutes skimming [doc://perldata] would clear things up. As I've learned other languages I've often wished those languages had such good documentation.</p>
<div class="pmsig"><div class="pmsig-281137">
<br /><p>Dave</p>
</div></div>
11109448
11109448