how to get the number of keys in an hash

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Liang

    how to get the number of keys in an hash

    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


  • Jürgen Exner

    #2
    Re: how to get the number of keys in an hash

    Liang wrote:[color=blue]
    > 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++;
    > }[/color]

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

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

    jue


    Comment

    • Jeff Dunn

      #3
      Re: how to get the number of keys in an hash

      "Liang" <leo2002chen@ho tmail.com> wrote in message news:<cgh23l$sf 4$1@newshost.mo t.com>...[color=blue]
      > 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[/color]


      Try

      $keyCount=(%key s %hash);

      Comment

      • nobull@mail.com

        #4
        Re: how to get the number of keys in an hash

        "Liang" <leo2002chen@ho tmail.com> wrote in message news:<cgh23l$sf 4$1@newshost.mo t.com>...[color=blue]
        > Hi,
        >
        > I need to get the number of keys in an hash. Is there any convenient way to
        > get it?[/color]

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

        No, that is not necesary.

        This newsgroup does not exist (see FAQ). Please do not start threads here.
        [color=blue]
        > Thanks in advance,[/color]

        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.

        Comment

        • Steve Bennett

          #5
          Re: how to get the number of keys in an hash

          Everyone says "See FAQ". Could you please include the URL to the FAQ?
          Be kind to newbies......Yo u used to be one.

          -s

          nobull@mail.com wrote:[color=blue]
          >
          > "Liang" <leo2002chen@ho tmail.com> wrote in message news:<cgh23l$sf 4$1@newshost.mo t.com>...[color=green]
          > > Hi,
          > >
          > > I need to get the number of keys in an hash. Is there any convenient way to
          > > get it?[/color]
          >
          > Yes, the keys() function.
          >[color=green]
          > > or use the following one?
          > > foreach $v (keys %hash){
          > > $count++;
          > > }[/color]
          >
          > No, that is not necesary.
          >
          > This newsgroup does not exist (see FAQ). Please do not start threads here.
          >[color=green]
          > > Thanks in advance,[/color]
          >
          > 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.[/color]

          Comment

          • Joe Smith

            #6
            Re: how to get the number of keys in an hash

            Steve Bennett wrote:
            [color=blue]
            > Everyone says "See FAQ". Could you please include the URL to the FAQ?[/color]

            What happens when you do a Google search on "perl faq"?
            Newbies are expected to know that Google is their friend.

            Comment

            • Jürgen Exner

              #7
              Re: how to get the number of keys in an hash

              Steve Bennett wrote:[color=blue]
              > Everyone says "See FAQ". Could you please include the URL to the FAQ?[/color]

              No need for a URL, it's on your own harddrive (after you installed Perl)
              Just type
              perldoc perlfaq
              [color=blue]
              > Be kind to newbies......Yo u used to be one.[/color]

              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


              Comment

              • nobull@mail.com

                #8
                Re: how to get the number of keys in an hash

                Steve Bennett rudely spits TOFU in may face:
                [color=blue]
                > Everyone says "See FAQ". Could you please include the URL to the FAQ?[/color]

                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.
                [color=blue]
                > Be kind to newbies.[/color]

                I am. Teaching newbies that selfishness is rewarded is _not_ a
                kindness.

                Comment

                Working...