how to get the number of keys in an hash

L

Liang

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
 
J

Jürgen Exner

Liang said:
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
 
J

Jeff Dunn

Liang said:
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);
 
N

nobull

Liang said:
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.
 
S

Steve Bennett

Everyone says "See FAQ". Could you please include the URL to the FAQ?
Be kind to newbies......You used to be one.

-s
 
J

Joe Smith

Steve said:
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.
 
J

Jürgen Exner

Steve said:
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
 
N

nobull

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top