Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: multiple values for one key in hash

by Athanasius (Monsignor)
on Nov 08, 2012 at 12:49 UTC ( #1002891=note: print w/ replies, xml ) Need Help??


in reply to Re: multiple values for one key in hash
in thread multiple values for one key in hash

Just dereference the array in a scalar context:

#! perl use strict; use warnings; my %families = (Flintstone => [ qw(Pebbles) ], Simpson => [ qw(Bart Lisa Maggie) ], Keaton => [ qw(Alex Mallory Jennifer Andy) ]); for my $name (keys %families) { my $num_children = @{ $families{$name} }; # <-- Here's where it + happens if ($num_children == 1) { print "There is $num_children child in the $name family\n"; } else { print "There are $num_children children in the $name family\n" +; } }

Hope that helps,

Athanasius <°(((><contra mundum


Comment on Re^2: multiple values for one key in hash
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (8)
As of 2014-12-18 07:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (44 votes), past polls