Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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

by davido (Cardinal)
on Nov 29, 2019 at 22:35 UTC ( [id://11109460]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11109460]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2024-03-28 17:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found