Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Counting number of keys in a hash.

by sschneid (Deacon)
on Jul 27, 2004 at 19:33 UTC ( [id://377827]=perlquestion: print w/replies, xml ) Need Help??

sschneid has asked for the wisdom of the Perl Monks concerning the following question:

Is there a 'quick & easy' way to count the number of keys in a hash without creating an (incremental) variable and mapping through it?

-s.

Replies are listed 'Best First'.
Re: Counting number of keys in a hash.
by gaal (Parson) on Jul 27, 2004 at 19:36 UTC
    my $count = keys %hash;
Re: Counting number of keys in a hash.
by sweetblood (Prior) on Jul 27, 2004 at 19:37 UTC
    try $hash_cnt = scalar keys %hash

    HTH

    Sweetblood

      Thanx from me too. I just had to do this today actually. I figured there was a better way than my quick solution of:

      my $counter = 0; $counter++ for keys %hash;

      --
      "A long habit of not thinking a thing wrong, gives it a superficial appearance of being right." -- Thomas Paine
      naChoZ

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2024-04-16 06:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found