Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: num keys of the hash within hash?

by muzakfetch (Scribe)
on May 26, 2009 at 17:09 UTC ( [id://766257]=note: print w/replies, xml ) Need Help??


in reply to num keys of the hash within hash?

Maybe this is what you're looking for:
#!/usr/bin/perl use warnings; use strict; my $hashref = { first => { foo => bar, bar => baz, flop => floodle }, second => { bar => foo, baz => bar, etc => plodle } }; foreach ( keys %{$hashref} ) { print $_ . " contains " . scalar(%{$hashref->{$_}) . " keys.\n"; }
keys returns a scalar of keys, which you can count with scalar. :-) edit: put a pretty space before keys :-)

Replies are listed 'Best First'.
Re^2: num keys of the hash within hash?
by AnomalousMonk (Archbishop) on May 26, 2009 at 18:20 UTC
    >perl -wMstrict -le "my $hashref = { first => { foo => 'bar', bar => 'baz', flop => 'floodle' }, second => { bar => 'foo', baz => 'bar', etc => 'plodle' }, }; foreach ( keys %{$hashref} ) { print qq{$_ contains } . scalar(%{$hashref->{$_}}) . qq{ keys.}; } " first contains 3/8 keys. second contains 3/8 keys.
    Please note:
    • proper string definitions of values of hashes;
    •  scalar(%{$hashref->{$_}}) vice  scalar(%{$hashref->{$_});
    • per Re^2: num keys of the hash within hash?, the stringization of a hash does not necessarily imply the number of keys of the hash.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-20 16:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found