On 10/19/07, Joseph L. Casale <[EMAIL PROTECTED]> wrote:

> Is there a way to compute the size of a hash?

Yes; use the keys() function in a scalar context to get the number of
key-value pairs in the hash.

  my $count = keys %hash;

> I have a hash of hashes of hashes etc... and need to know how
> many items exist in the second hash. I know they are always
> named as integers from 0 up.

Now it sounds as if you're talking about arrays. Hash elements aren't
numbered, but array indices start at zero.

> Does such a method exist, or is there a better way? Could I
> reliably just start the loop and expect a certain error I could
> trap and stop on once I increment the hash value up to far
> instead?

To iterate through an entire hash, use while/each:

  my %hash = qw/ Chicago 25 Atlanta 18 Portland 29 /;
  while (my($key, $value) = each %hash) {
    print "The key is $key and the value is $value.\n";
  }

But it still sounds as if you're really asking about an array. If
you're iterating through an array, use foreach.

Hope this helps!

--Tom Phoenix
Stonehenge Perl Training

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to