Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
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 chanting in the Monastery: (6)
As of 2014-12-28 23:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

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





    Results (183 votes), past polls