Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > how to get the number of keys in an hash

Reply
Thread Tools

how to get the number of keys in an hash

 
 
Liang
Guest
Posts: n/a
 
      08-25-2004
Hi,

I need to get the number of keys in an hash. Is there any convenient way to
get it?

or use the following one?
foreach $v (keys %hash){
$count++;
}

Thanks in advance,
Liang


 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      08-25-2004
Liang wrote:
> Hi,
>
> I need to get the number of keys in an hash. Is there any convenient
> way to get it?
>
> or use the following one?
> foreach $v (keys %hash){
> $count++;
> }


Oh dear, that's a rather creative way....

Trivial. Just use "keys(%hash)" in scalar context, e.g.
my $number = keys %hash;

jue


 
Reply With Quote
 
 
 
 
Jeff Dunn
Guest
Posts: n/a
 
      08-25-2004
"Liang" <(E-Mail Removed)> wrote in message news:<cgh23l$sf4$(E-Mail Removed)>...
> Hi,
>
> I need to get the number of keys in an hash. Is there any convenient way to
> get it?
>
> or use the following one?
> foreach $v (keys %hash){
> $count++;
> }
>
> Thanks in advance,
> Liang



Try

$keyCount=(%keys %hash);
 
Reply With Quote
 
nobull@mail.com
Guest
Posts: n/a
 
      08-25-2004
"Liang" <(E-Mail Removed)> wrote in message news:<cgh23l$sf4$(E-Mail Removed)>...
> Hi,
>
> I need to get the number of keys in an hash. Is there any convenient way to
> get it?


Yes, the keys() function.

> or use the following one?
> foreach $v (keys %hash){
> $count++;
> }


No, that is not necesary.

This newsgroup does not exist (see FAQ). Please do not start threads here.

> Thanks in advance,


If you are going to do any things "in advance" then my suggestions would be:

1) Check the FAQ.
2) Check the documentation for the functions you are using.
 
Reply With Quote
 
Steve Bennett
Guest
Posts: n/a
 
      08-27-2004
Everyone says "See FAQ". Could you please include the URL to the FAQ?
Be kind to newbies......You used to be one.

-s

http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
>
> "Liang" <(E-Mail Removed)> wrote in message news:<cgh23l$sf4$(E-Mail Removed)>...
> > Hi,
> >
> > I need to get the number of keys in an hash. Is there any convenient way to
> > get it?

>
> Yes, the keys() function.
>
> > or use the following one?
> > foreach $v (keys %hash){
> > $count++;
> > }

>
> No, that is not necesary.
>
> This newsgroup does not exist (see FAQ). Please do not start threads here.
>
> > Thanks in advance,

>
> If you are going to do any things "in advance" then my suggestions would be:
>
> 1) Check the FAQ.
> 2) Check the documentation for the functions you are using.

 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      08-27-2004
Steve Bennett wrote:

> Everyone says "See FAQ". Could you please include the URL to the FAQ?


What happens when you do a Google search on "perl faq"?
Newbies are expected to know that Google is their friend.
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      08-27-2004
Steve Bennett wrote:
> Everyone says "See FAQ". Could you please include the URL to the FAQ?


No need for a URL, it's on your own harddrive (after you installed Perl)
Just type
perldoc perlfaq

> Be kind to newbies......You used to be one.


You also seem to be new to Usenet. It is considered rude to
- top post
- blindly full quote
For an explanation of what that is and why it's considered rude please
search www.google.com

jue


 
Reply With Quote
 
nobull@mail.com
Guest
Posts: n/a
 
      08-27-2004
Steve Bennett rudely spits TOFU in may face:

> Everyone says "See FAQ". Could you please include the URL to the FAQ?


You are supposed to look for a FAQ _before_ you ask a question. It
would defeat the purpose if the FAQ was so hard to find that asking
where it was was a ligitimate question. Anything you could reasonably
imagine doing to locate the Perl FAQ will succede in about fifteen
seconds.

> Be kind to newbies.


I am. Teaching newbies that selfishness is rewarded is _not_ a
kindness.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#keys, Hash#values order question Ronald Fischer Ruby 0 08-23-2007 09:34 AM
hash.keys and hash.values Mage Ruby 14 08-15-2006 08:44 PM
Hash#values and Hash#keys order Alex Fenton Ruby 1 04-15-2006 05:45 AM
No Keys, nor other hash methods on multidimensional hash Xeno Campanoli Ruby 16 08-25-2005 07:04 AM



Advertisments