469,366 Members | 2,198 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,366 developers. It's quick & easy.

how to get the number of keys in an hash

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
Jul 19 '05 #1
7 32885
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
Jul 19 '05 #2
"Liang" <le*********@hotmail.com> wrote in message news:<cg**********@newshost.mot.com>...
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);
Jul 19 '05 #3
"Liang" <le*********@hotmail.com> wrote in message news:<cg**********@newshost.mot.com>...
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.
Jul 19 '05 #4
Everyone says "See FAQ". Could you please include the URL to the FAQ?
Be kind to newbies......You used to be one.

-s

no****@mail.com wrote:

"Liang" <le*********@hotmail.com> wrote in message news:<cg**********@newshost.mot.com>...
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.

Jul 19 '05 #5
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.
Jul 19 '05 #6
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
Jul 19 '05 #7
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.
Jul 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by amywolfie | last post: by
6 posts views Thread by Jovo Mirkovic | last post: by
24 posts views Thread by kdotsky | last post: by
139 posts views Thread by ravi | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.