Re: FAQ 4.58 How can I know how many entries are in a hash?



On Thu, 13 Dec 2007 05:01:22 -0800, Todd wrote:

$num_keys = keys %hash;

Seems there is a better way to do it:

#! /bin/perl -l

%hash = (a=>1, b=>2, c=>3, d=>4, e);
$num_keys = %hash+0;

I fail to see how this qualifies as "better".

[..]

So it means when in numeric(not string) context, %hash return numbers of
keys accidently. But anyway, it's a good feature.

This is indeed accidental, and as far as I know not guaranteed to work in
any future version of perl. It's also unlikely to work with tied hashes.

Joost.




.



Relevant Pages

  • Re: [PATCH] cgroup : remove the ns_cgroup
    ... let's fail with EINVAL when the ns_cgroup ... Update the feature remove schedule file with the deprecated ns_cgroup. ... notify ns_cgroup deprecated") caused the kernel ...
    (Linux-Kernel)
  • Re: Recruiters who place people first...
    ... Some might consider this a feature, not a bug. ... The only class I failed was some sporting physical education class. ... they should all fail in the same way. ...
    (alt.sysadmin.recovery)
  • Re: csh question: foreach
    ... >>Unices but may fail on Solaris. ... Could you explain what sh feature ... start your script with #!/bin/sh, then your script will work only ...
    (comp.unix.shell)
  • Re: dict literals vs dict(**kwds)
    ... sugar. ... The thing is there are four ways to get a dict instance: ... The fact that you fail to understand why a given feature ...
    (comp.lang.python)
  • Re: csh question: foreach
    ... >>Unices but may fail on Solaris. ... Could you explain what sh feature ... start your script with #!/bin/sh, then your script will work only ...
    (comp.unix.solaris)