# Tell me the Flintstones HoH has 22 things

#### Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

•  Subject
• Author
• Posted on
Regarding this hash of hashes on
\$ man perldsc
%HoH = (
flintstones => {
pal       => "barney",
},
jetsons     => {
wife      => "jane",
"his boy" => "elroy",
},
simpsons    => {
wife      => "marge",
kid       => "bart",
},
);

What command will tell me what I want to know: that there are 22 things
inside it? The following docs didn't help. Does one really need to write
a recursive program to finally get a "22"? I'm willing to take 19 for an
answer too, but not 3, or "2/8".

\$ perldoc -q entries

No I don't!!

elements==keys?!

## Re: Tell me the Flintstones HoH has 22 things

jidanni@jidanni.org writes:

Ummm ... the only anwers I can see would be "3" (there are three things
in %HoH) or 8 (there are eight leaves at the bottom of the tree.)

How you came to 22 is a bogglement ....

--L

## Re: Tell me the Flintstones HoH has 22 things

LS> jidanni@jidanni.org writes:
>> Regarding this hash of hashes on
>> \$ man perldsc
>> %HoH = (
>> flintstones => {
>> pal       => "barney",
>> },
>> jetsons     => {
>> wife      => "jane",
>> "his boy" => "elroy",
>> },
>> simpsons    => {
>> wife      => "marge",
>> kid       => "bart",
>> },
>> );

LS> How you came to 22 is a bogglement ....

there are 3 top level keys (or 6 things counting keys and values) and
then 8 second level keys (16 more things) for a total of 22. yes, it is
a very odd and useless number. and it is easily derived by counting keys
recursively and doubling that value. what use this has is beyond me.

uri

--
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------

## Re: Tell me the Flintstones HoH has 22 things

I was being disingenuous -- I was able to figure out what there were 22
of ... The part that is still a bogglement is why does OP consider a key
and its associated value separate "things" , and - as you pointed out -
what possible meaning could that number have?  I did head over to google
and search OP's posting history, and I think he's just antisocial and
trollish.

--L

## Re: Tell me the Flintstones HoH has 22 things

LS> How you came to 22 is a bogglement ....
>>
>> there are 3 top level keys (or 6 things counting keys and values) and
>> then 8 second level keys (16 more things) for a total of 22. yes, it is
>> a very odd and useless number. and it is easily derived by counting keys
>> recursively and doubling that value. what use this has is beyond me.
>>
>> uri

LS> I was being disingenuous -- I was able to figure out what there were 22
LS> of ... The part that is still a bogglement is why does OP consider a key
LS> and its associated value separate "things" , and - as you pointed out -
LS> what possible meaning could that number have?  I did head over to google
LS> and search OP's posting history, and I think he's just antisocial and
LS> trollish.

sounds good to me. and scanning a tree is basic data structure coding
that is never a builtin in any lang i have heard of as it is way too
high level. let alone counting 'things' in the tree being a perl
builtin.

uri

--
Uri Guttman  ------  uri@stemsystems.com  --------  http://www.sysarch.com --
-----  Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
---------  Gourmet Hot Cocoa Mix  ----  http://bestfriendscocoa.com ---------

## Re: Tell me the Flintstones HoH has 22 things

Agreed -- I came away with a distinct feeling of "WAHHHHHHHHHHHHHH!
Perl is the sux0r!  it doesn't solve some imaginary problem I've just
made up!  You all suck.  I'm going to go write all my code in
interpreted object-oriented COBOL!"

--L

## Re: Tell me the Flintstones HoH has 22 things

On Fri, 05 Jun 2009 11:51:30 +0800, jidanni@jidanni.org wrote:

yes

-sln

## Re: Tell me the Flintstones HoH has 22 things

On Thu, 04 Jun 2009 21:12:02 -0700, sln@netherlands.com wrote:

The question is, can you write a 3-liner to do it?

## Re: Tell me the Flintstones HoH has 22 things

Yes, you really need to recurse down the structure.  It's probable that
something like this exists in CPAN already, though.  You might try
using Data::Walk:

http://search.cpan.org/~guido/Data-Walk-1.00/lib/Data/Walk.pm

There is a one-to-one mapping between the keys of a hash and its values.
The above structure just happens to have hashrefs as the values of the
hash.  But scalar keys %hash doesn't know (or care) what the values are;
in fact, scalar values %hash would give you the same number (or at least
it should).

So yes, you need to recurse your structure if you want to know how many
actual scalar values (as opposed to references) are in your hash and its
substructures.

--keith

--
kkeller-usenet@wombat.san-francisco.ca.us
(try just my userid to email me)
AOLSFAQ=http://www.therockgarden.ca/aolsfaq.txt
see X- headers for PGP signature information

## Re: Tell me the Flintstones HoH has 22 things

On Jun 4, 9:16=A0pm, Keith Keller <kkeller-use...@wombat.san-
francisco.ca.us> wrote:

Easy File::Find-like interface too:

use Data::Walk;
...
my \$count = 0;
walkdepth( sub , %HoH );
print "count=3D\$count\m";

--
Charles DeRykus

## Re: FAQ 4.59 How can I know how many entries are in a hash?

If you mean ALL the entries, even recursively, then try e.g.,

use Data::Walk; #from CPAN
my \$count = 0;
walkdepth( sub , %HoH );
print "count=\$count\n";

## Re: FAQ 4.59 How can I know how many entries are in a hash?

jidanni@jidanni.org wrote:
)> 4.59: How can I know how many entries are in a hash?
)>
)>     If you mean how many keys, then all you have to do is use the keys()
)>     function in a scalar context:
)>
)>         \$num_keys = keys %hash;
)>
)>     The keys() function also resets the iterator, which means that you may
)>     see strange results if you use this between uses of other hash operators
)>     such as each().
)
)
) If you mean ALL the entries, even recursively, then try e.g.,
)
) use Data::Walk; #from CPAN
) my \$count = 0;
) walkdepth( sub , %HoH );
) print "count=\$count\n";

There's nothing F about the A-ing of that Q.

SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT

## Re: FAQ 4.59 How can I know how many entries are in a hash?

f you mean ALL the entries, even recursively, then try e.g.,

As I noted to jidanni in private email, the code doesn't even work.
The Data::Walk module doesn't have any way to tell you how many
keys it saw. It would take a lot of surgery to give it that capability.
I gave up on it after about half an hour.

Too bad too, because that would have been a nice feature.

## Re: FAQ 4.59 How can I know how many entries are in a hash?

Since the FAQ question is "How can I know how many entries
are in a hash?", why not just replace "entries" with "keys"
in the question itself and avoid any ambiguity; or, replace
"hash" with the more general "data structure" and keep the reference
to Data::Walk since someone might then reasonably
infer entries to be a total count of nodes.

--
Charles DeRykus

## Re: FAQ 4.59 How can I know how many entries are in a hash?

^^^^^^^^^

I'd suggest using something else there to avoid potential scoring-down

^^^^^^
^^^^^^

The ambiguity doesn't come from entries vs keys.

It comes from "a hash" meaning meaning "a hash and all of its sub-hashes".

As I recall, the OP had 3 hashes, 2 sub-hashes inside of a parent
hash and he didn't want the count for "a hash" but rather for all
of them together.

--
email: perl -le "print scalar reverse qq/moc.noitatibaher0cmdat/"

## Re: FAQ 4.59 How can I know how many entries are in a hash?

Yup, thanks.

No, I don't agree.  He wasn't interested in just the count of keys in
the top-level or even all the sub-
levels  --  but rather a  count of all nodes for the entire data
structure. That's why  Data::Walk was suggested to get the count. It
was an unusual request
of course but the ambiguity related at least in part to what
constitutes a count of the "entries" in a data structure.

--
Charles DeRykus

## Re: FAQ 4.59 How can I know how many entries are in a hash?

Note also the slight of hand with "elements" becoming "keys", and also
"entire" vs. top level. Wish somebody would fix it. Not newbie me.:
\$ perldoc -f length
...Note that this cannot be used on an entire array or hash to find out
how many elements these have. For that, use "scalar @array" and
"scalar keys %hash" respectively.

## Re: FAQ 4.59 How can I know how many entries are in a hash?

What do you think needs fixing in that statement?

The "elements" here refers to members of either an array or a hash.
Hash elements have (key,value) pairs, so the number of elements is the
number of keys, and the number of elements is given by 'scalar keys
%hash'. Arrays don't have keys, so the number of elements is given by
'scalar @array'.

There is no 'slight of hand', just the use of the 'keys' operator to
find the number of 'elements' in a hash.

--
Jim Gibson

## Re: FAQ 4.59 How can I know how many entries are in a hash?

JG> What do you think needs fixing in that statement?

JG> The "elements" here refers to members of either an array or a hash.
JG> Hash elements have (key,value) pairs, so the number of elements is the
JG> number of keys, and the number of elements is given by 'scalar keys
JG> %hash'. Arrays don't have keys, so the number of elements is given by
JG> 'scalar @array'.

JG> There is no 'slight of hand', just the use of the 'keys' operator to
JG> find the number of 'elements' in a hash.

The documentation tries to be brief and merges "hash elements" with
"array elements," which can be confusing to an inexperienced programmer.

My suggestion, FWIW (also eliminates "respectively" which is always
confusing, and some other minor tweaks):

length  Returns the length in *characters* of the value of EXPR. If EXPR
is omitted, returns length of \$_. Note that this cannot be used
on an entire array or hash to find out how many elements these
have. For that, use "scalar @array" and "scalar keys %hash"
respectively.

should be

length Returns the length in *characters* of the value of EXPR. If
EXPR is omitted, returns length of \$_. Note that this cannot
be used directly on an entire array or hash to find their
size. Use "scalar @array" to count array elements and "scalar
keys %hash" to count hash entries.

Ted

## Re: FAQ 4.59 How can I know how many entries are in a hash?

There is a one-to-one relationship between a hash key and a hash element.

If you know how many hash keys there are, then you also know how
many elements there are.

my %hash = (
one => 1,
two => 2,
three => 3,
);

Three hash keys, three elements.

If you insist there must be six elements there, then multiply the
number of keys by two. Though I cannot think of a practical reason
for needing that...

There is a one-to-one relationship between an array index and an array element.

If you know how many array indexes there are, then you also know how
many elements there are.

It is not broken.

It seems to be your mental model that needs to be fixed.

--