in reply to Re: scalar subkeys %hash ?
in thread scalar subkeys %hash ?
Hey, that's was pretty slick. crazysniffable, if you're not sure what's going on, here's the scoop:
The map operator takes an array and loops over each element. In this case, the array is the keys of %hash. In srawls' code for map, he's asking for it to return the keys to the anonymous hash at $hash{$_}. Since he's evaluating it in scalar context, it returns the total number of elements, instead of the keys. This code is equivalent to:
Of course, as srawls mentioned, it loops. There's really no way around this (that I can see).my $numSubKeys = 0; foreach ( keys %hash ) { $numSubKeys += scalar keys %{$hash{$_}} }; }
Cheers,
Ovid
Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: (Ovid) Re: Re: scalar subkeys %hash ?
by crazysniffable (Acolyte) on Jun 14, 2001 at 05:15 UTC | |
by tye (Sage) on Jun 14, 2001 at 10:54 UTC | |
by crazysniffable (Acolyte) on Jun 15, 2001 at 02:49 UTC |