On Mon, 4 Jun 2001, Doug Johnson wrote:

> In an array to find the number of elements in an array I :
>
> print "$#array\n";

Nope, not quite.  It returns the index of the last element.  Putting your
list in a scalar context is a better way:

my $array_cnt = @array

This is what foreach does when you do this:

foreach(0..@array) {

> 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?

Use the keys function -- it returns the number of keys when used in a
scalar context:

my $key_count = keys %HASH;

See 'perldoc -f keys' for more details.

-- Brett

Brett W. McCoy
Software Engineer
Broadsoft, Inc.
240-364-5225
[EMAIL PROTECTED]

Reply via email to