Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Counting hash of hashes elements

by Aragorn (Curate)
on Nov 01, 2004 at 11:25 UTC ( #404273=note: print w/ replies, xml ) Need Help??


in reply to Counting hash of hashes elements

Here's a way:

#!/usr/local/bin/perl use strict; use warnings; my %hash = ( 'a' => { 'x' => 40, 'z' => 102 }, 'b' => { 'z' => 100, 'x' => 10, 'y' => 20 }, 'c' => { 'x' => 50 }, 'd' => { 'z' => 101, 'y' => 30 } ); my $x_but_not_y = 0; my $z = 0; foreach my $key (keys %hash) { $x_but_not_y++ if defined $hash{$key}->{x} and not defined $hash{$ +key}->{y}; $z++ if defined $hash{$key}->{z}; } print "number of keys where x defined, but not y: $x_but_not_y\n"; print "number of keys where z defined: $z\n";
Arjen


Comment on Re: Counting hash of hashes elements
Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2015-07-04 04:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (57 votes), past polls