in reply to hash count

scalar grep defined($_), values %hash

If you don't want to grep for defined values, delete the values instead of setting them to undef, then you can the output of scalar %hash.

See perldata on what exactly the scalar value of a hash is.

Update: fixed syntax error. Sorry for not writing the update notice in the first place.

Replies are listed 'Best First'.
Re^2: hash count
by kyle (Abbot) on Mar 25, 2009 at 14:51 UTC

    I usually use "scalar keys %hash" to find how many pairs are in a %hash.

    scalar grep defined($_) values %hash

    I think that should be:

    scalar grep defined, values %hash;

    or

    scalar grep { defined } values %hash;
          scalar grep defined($_) values %hashI think that should be: ...
      It could be as you have written it, but I think moritz may have been considering the relative inexperience of the OPer by passing the parameter explicitly rather than implicitly.

      Also, the code fragment was
          scalar grep defined($_), values %hash
      (comma present after the defined($_) built-in call) in moritz's original reply (unless moritz has been updating replies without noting the fact – unlikely!).

        (unless moritz has been updating replies without noting the fact – unlikely!).

        I did, mea culpa.

        (I sometimes do that when the node is still very new and there are no replies and it's a small change. But if it causes confusion, I try to clarify it).

      Yes, I forgot the comma after the defined, and updated my post. Thanks to ikegami for noting it.