I would recommend the other answers over my own.

 my $cnt = scalar keys %hash;

seems more readable to me.

Paul

--- Doug Johnson <[EMAIL PROTECTED]> wrote:
> Perfect!  Thanks a million..
> 
> Doug
> 
> 
> 
> --- Doug Johnson <[EMAIL PROTECTED]> wrote:
> > In an array to find the number of elements in an array I :
> > 
> > print "$#array\n"; 
> > 
> > Is there a similar way to find the number of keys in a hash without
> > cycling through them and without assigning the hash to an array?
> 
> You can print the hash in a scalar context:
> 
>  %a = (a=>1,b=>2,c=>3);
>  print scalar %a;
> 
> This results in 
> 
>  3/8
> 
> Which means there are 3 elements, and space has been allocated for 8.
> 
> You can also do something like:
> 
>  %a = (a=>1,b=>2,c=>3);
>  print scalar @{ [ %a ] } / 2; 
> 
> Since [ %a ] will creates an anonymous reference to an array
> containing
> all the keys AND values of %a, and evaluating that ref as an array in
> a
> scalar contaxt tells you how many elements there were. Dividing that
> by
> two gives you a count of the keys.
> 
> I'd do the first one, and just split it on the slash.
> 
>  %a = (a=>1,b=>2,c=>3);
>  ($elem) = split m[/], scalar %a; 
>  print "$elem\n";
> 
> __________________________________________________
> Do You Yahoo!?
> Get personalized email addresses from Yahoo! Mail - only $35 
> a year!  http://personal.mail.yahoo.com/


__________________________________________________
Do You Yahoo!?
Get personalized email addresses from Yahoo! Mail - only $35 
a year!  http://personal.mail.yahoo.com/

Reply via email to