Gerryjun has asked for the wisdom of the Perl Monks concerning the following question:

Getting the number of fields in hash per key Help! I know how to get the list length "$total = $#h+1;"
on a @List, but now i would want to do the same in hash per given key.

how can i get or know the number of entries a hash has? exp.
 $h{'key'}

Replies are listed 'Best First'.
Re: getting the number of fields in hash per key
by runrig (Abbot) on Nov 10, 2001 at 01:47 UTC
    One of these should do what you want:
    # index of last element print $#{$h{a}},"\n"; # number of elements my $num = @{$h{a}}; print "$num\n";
Re: getting the number of fields in hash per key
by mkmcconn (Chaplain) on Nov 10, 2001 at 03:01 UTC

    It might be bad form (I'd like other opinions), but I habitually do this:

    print 0+(keys %h);
    mkmcconn

    apologies - read it too quickly. Same idea then,

    0+@{$h{'key'}}
    sorry again *sheesh*

Re: getting the number of fields in hash per key
by hopes (Friar) on Nov 10, 2001 at 04:39 UTC
    Also using scalar
    scalar keys %{$hash{key}}
    Greets
    Hopes