how to get the number of keys in an hash

Discussion in 'Perl' started by Liang, Aug 25, 2004.

  1. Liang

    Liang Guest

    Hi,

    I need to get the number of keys in an hash. Is there any convenient way to
    get it?

    or use the following one?
    foreach $v (keys %hash){
    $count++;
    }

    Thanks in advance,
    Liang
     
    Liang, Aug 25, 2004
    #1
    1. Advertising

  2. Liang wrote:
    > Hi,
    >
    > I need to get the number of keys in an hash. Is there any convenient
    > way to get it?
    >
    > or use the following one?
    > foreach $v (keys %hash){
    > $count++;
    > }


    Oh dear, that's a rather creative way....

    Trivial. Just use "keys(%hash)" in scalar context, e.g.
    my $number = keys %hash;

    jue
     
    Jürgen Exner, Aug 25, 2004
    #2
    1. Advertising

  3. Liang

    Jeff Dunn Guest

    "Liang" <> wrote in message news:<cgh23l$sf4$>...
    > Hi,
    >
    > I need to get the number of keys in an hash. Is there any convenient way to
    > get it?
    >
    > or use the following one?
    > foreach $v (keys %hash){
    > $count++;
    > }
    >
    > Thanks in advance,
    > Liang



    Try

    $keyCount=(%keys %hash);
     
    Jeff Dunn, Aug 25, 2004
    #3
  4. Liang

    Guest

    "Liang" <> wrote in message news:<cgh23l$sf4$>...
    > Hi,
    >
    > I need to get the number of keys in an hash. Is there any convenient way to
    > get it?


    Yes, the keys() function.

    > or use the following one?
    > foreach $v (keys %hash){
    > $count++;
    > }


    No, that is not necesary.

    This newsgroup does not exist (see FAQ). Please do not start threads here.

    > Thanks in advance,


    If you are going to do any things "in advance" then my suggestions would be:

    1) Check the FAQ.
    2) Check the documentation for the functions you are using.
     
    , Aug 25, 2004
    #4
  5. Everyone says "See FAQ". Could you please include the URL to the FAQ?
    Be kind to newbies......You used to be one.

    -s

    wrote:
    >
    > "Liang" <> wrote in message news:<cgh23l$sf4$>...
    > > Hi,
    > >
    > > I need to get the number of keys in an hash. Is there any convenient way to
    > > get it?

    >
    > Yes, the keys() function.
    >
    > > or use the following one?
    > > foreach $v (keys %hash){
    > > $count++;
    > > }

    >
    > No, that is not necesary.
    >
    > This newsgroup does not exist (see FAQ). Please do not start threads here.
    >
    > > Thanks in advance,

    >
    > If you are going to do any things "in advance" then my suggestions would be:
    >
    > 1) Check the FAQ.
    > 2) Check the documentation for the functions you are using.
     
    Steve Bennett, Aug 27, 2004
    #5
  6. Liang

    Joe Smith Guest

    Steve Bennett wrote:

    > Everyone says "See FAQ". Could you please include the URL to the FAQ?


    What happens when you do a Google search on "perl faq"?
    Newbies are expected to know that Google is their friend.
     
    Joe Smith, Aug 27, 2004
    #6
  7. Steve Bennett wrote:
    > Everyone says "See FAQ". Could you please include the URL to the FAQ?


    No need for a URL, it's on your own harddrive (after you installed Perl)
    Just type
    perldoc perlfaq

    > Be kind to newbies......You used to be one.


    You also seem to be new to Usenet. It is considered rude to
    - top post
    - blindly full quote
    For an explanation of what that is and why it's considered rude please
    search www.google.com

    jue
     
    Jürgen Exner, Aug 27, 2004
    #7
  8. Liang

    Guest

    Steve Bennett rudely spits TOFU in may face:

    > Everyone says "See FAQ". Could you please include the URL to the FAQ?


    You are supposed to look for a FAQ _before_ you ask a question. It
    would defeat the purpose if the FAQ was so hard to find that asking
    where it was was a ligitimate question. Anything you could reasonably
    imagine doing to locate the Perl FAQ will succede in about fifteen
    seconds.

    > Be kind to newbies.


    I am. Teaching newbies that selfishness is rewarded is _not_ a
    kindness.
     
    , Aug 27, 2004
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. rp
    Replies:
    1
    Views:
    585
    red floyd
    Nov 10, 2011
  2. Xeno Campanoli
    Replies:
    16
    Views:
    286
    Martin DeMello
    Aug 25, 2005
  3. Alex Fenton

    Hash#values and Hash#keys order

    Alex Fenton, Apr 7, 2006, in forum: Ruby
    Replies:
    1
    Views:
    162
    George Ogata
    Apr 15, 2006
  4. Mage

    hash.keys and hash.values

    Mage, Aug 13, 2006, in forum: Ruby
    Replies:
    14
    Views:
    194
  5. Ronald Fischer

    Hash#keys, Hash#values order question

    Ronald Fischer, Aug 23, 2007, in forum: Ruby
    Replies:
    0
    Views:
    172
    Ronald Fischer
    Aug 23, 2007
Loading...

Share This Page