[newbie] how to get just the first value of a hash??

Discussion in 'Perl Misc' started by Janek Schleicher, Sep 3, 2003.

  1. Guest1 wrote at Wed, 03 Sep 2003 14:52:22 +0100:

    You can't.
    Hashs are unsorted, so there is no first key or value.


    Greetings,
    Janek
     
    Janek Schleicher, Sep 3, 2003
    #1
    1. Advertising

  2. Guest1 wrote at Wed, 03 Sep 2003 14:52:22 +0100:

    If you want to loop over the values of a hash,
    you can either use the list of all values with

    my $value = (values %hash)[0];

    or iterate through with

    while (my ($key, $value) = each %hash) {
    # do something with them
    }

    However, the returned value is in general not the same as the first value
    you've written into the hash.


    Greetings,
    Janek
     
    Janek Schleicher, Sep 3, 2003
    #2
    1. Advertising

  3. Janek Schleicher

    Guest1 Guest

    Thanks in advance
    Pedro
     
    Guest1, Sep 3, 2003
    #3
  4. Janek Schleicher

    Bigus Guest

    There is no "first value of a hash". That is, a hash just consists of keys
    and values. If you want the lowest value in a hash, assuming the values are
    all numbers of course, then you could do it something like this:

    @keys = sort {$hash{$a} <=> $hash{$b}} keys %hash;
    print $hash{$keys[0]};

    Bigus
     
    Bigus, Sep 3, 2003
    #4
  5. Janek Schleicher

    Mina Naguib Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Guest1 wrote:

    In the future, please post your question in the body of your message, not just the subject.

    > Thanks in advance
    > Pedro


    Unlike arrays, hashes are not an ordered list, therefore there's no concept of "first".

    Think of a hash as a way to describe someone's face for example. One property is eye colour,
    another is hair length, etc. None of the properties are first or last or more important than the other.

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.1 (GNU/Linux)
    Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

    iD8DBQE/VfVieS99pGMif6wRAlP1AJ9hD06J8DtMctfiJWNjpD1HlJt9xwCgyFGx
    jmMWbG/iGJMYyI/BXxfkkLw=
    =+nrC
    -----END PGP SIGNATURE-----
     
    Mina Naguib, Sep 3, 2003
    #5
  6. Mina Naguib wrote:

    > -----BEGIN PGP SIGNED MESSAGE-----
    > Hash: SHA1
    >
    > Guest1 wrote:
    >
    > In the future, please post your question in the body of your message, not
    > just the subject.
    >
    >> Thanks in advance
    >> Pedro

    >
    > Unlike arrays, hashes are not an ordered list, therefore there's no
    > concept of "first".
    >
    > Think of a hash as a way to describe someone's face for example. One
    > property is eye colour,
    > another is hair length, etc. None of the properties are first or last or
    > more important than the other.


    Ack.

    > -----BEGIN PGP SIGNATURE-----
    > Version: GnuPG v1.2.1 (GNU/Linux)
    > Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
    >
    > iD8DBQE/VfVieS99pGMif6wRAlP1AJ9hD06J8DtMctfiJWNjpD1HlJt9xwCgyFGx
    > jmMWbG/iGJMYyI/BXxfkkLw=
    > =+nrC
    > -----END PGP SIGNATURE-----


    Is this PGP-sig really nescessary on usenet? It's ugly like hell. And a lot
    of work if you want to remove it on reply.
    --
    Blood, sweat & tears
     
    Gunter Schelfhout, Sep 3, 2003
    #6
  7. On Wed, 03 Sep 2003 14:52:22 +0100, Guest1 wrote:

    >
    > Thanks in advance
    > Pedro


    define first

    - Brian
     
    Brian Harnish, Sep 3, 2003
    #7
  8. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    On Wed, 03 Sep 2003 14:14:26 +0000, Gunter Schelfhout wrote:

    > Mina Naguib wrote:

    [snip...]
    >> -----BEGIN PGP SIGNATURE-----
    >> Version: GnuPG v1.2.1 (GNU/Linux)
    >> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
    >>
    >> iD8DBQE/VfVieS99pGMif6wRAlP1AJ9hD06J8DtMctfiJWNjpD1HlJt9xwCgyFGx
    >> jmMWbG/iGJMYyI/BXxfkkLw=
    >> =+nrC
    >> -----END PGP SIGNATURE-----

    >
    > Is this PGP-sig really nescessary on usenet? It's ugly like hell. And a lot
    > of work if you want to remove it on reply.


    Yes, it is nescessary, it's the only way of telling who really sent the
    message. And, the signatures are roughly the acceptable size for usenet
    ..sig's.

    - Brian Harnish ()
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.2 (GNU/Linux)

    iD8DBQE/Vf9RiK/rA3tCpFYRAnLaAJ9v8G2m44TI61KxVoCjmLwW6AOsugCgmzSv
    BDUAWRPfYJqdoTUfOVdhEw0=
    =oNJC
    -----END PGP SIGNATURE-----
     
    Gunter Schelfhout, Sep 3, 2003
    #8
  9. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Gunter Schelfhout wrote:

    > -----BEGIN PGP SIGNED MESSAGE-----
    > Hash: SHA1
    >
    > On Wed, 03 Sep 2003 14:14:26 +0000, Gunter Schelfhout wrote:
    >
    >> Mina Naguib wrote:

    > [snip...]
    >>> -----BEGIN PGP SIGNATURE-----
    >>> Version: GnuPG v1.2.1 (GNU/Linux)
    >>> Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
    >>>
    >>> iD8DBQE/VfVieS99pGMif6wRAlP1AJ9hD06J8DtMctfiJWNjpD1HlJt9xwCgyFGx
    >>> jmMWbG/iGJMYyI/BXxfkkLw=
    >>> =+nrC
    >>> -----END PGP SIGNATURE-----

    >>
    >> Is this PGP-sig really nescessary on usenet? It's ugly like hell. And a
    >> lot of work if you want to remove it on reply.


    blabla.

    > Yes, it is nescessary, it's the only way of telling who really sent the
    > message. And, the signatures are roughly the acceptable size for usenet
    > .sig's.


    blabla

    > - Brian Harnish ()
    > -----BEGIN PGP SIGNATURE-----
    > Version: GnuPG v1.2.2 (GNU/Linux)
    >
    > iD8DBQE/Vf9RiK/rA3tCpFYRAnLaAJ9v8G2m44TI61KxVoCjmLwW6AOsugCgmzSv
    > BDUAWRPfYJqdoTUfOVdhEw0=
    > =oNJC
    > -----END PGP SIGNATURE-----


    I know where it is used for. IMHO, it doesn't make sense to use it on
    usenet.
    Look at the mess after only a couple of messages.
    - --
    Blood, sweat & tears
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.0.7 (GNU/Linux)

    iD8DBQE/VgUrtGqwJj9zNs4RAtMEAJ4mgu8utdi1uG7bFQKw8GdhfnekQACdGwfT
    T4DXvc3eAxdt89zOXZlK6lc=
    =yj94
    -----END PGP SIGNATURE-----
     
    Gunter Schelfhout, Sep 3, 2003
    #9
  10. Janek Schleicher

    Guest1 Guest

    I have a hash of arrays, and I would like to get the length of any of
    these arrays. I said first instead to say any of them (because all the
    arrays have the same length), More clear my question now?

    On the future I will try to be more clear from the begining.

    Thanks
    P

    On Wed, 3 Sep 2003, Brian Harnish wrote:

    > On Wed, 03 Sep 2003 14:52:22 +0100, Guest1 wrote:
    >
    > >
    > > Thanks in advance
    > > Pedro

    >
    > define first
    >
    > - Brian
    >
     
    Guest1, Sep 3, 2003
    #10
  11. Janek Schleicher

    Uri Guttman Guest

    >>>>> "G" == Guest1 <> writes:

    <don't top post>

    G> I have a hash of arrays, and I would like to get the length of any of
    G> these arrays. I said first instead to say any of them (because all the
    G> arrays have the same length), More clear my question now?

    perldoc -f each

    G> On the future I will try to be more clear from the begining.

    that is always helpful

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Damian Conway Class in Boston - Sept 2003 -- http://www.stemsystems.com/class
     
    Uri Guttman, Sep 3, 2003
    #11
  12. Guest1 wrote:
    > I have a hash of arrays, and I would like to get the length of any
    > of these arrays. I said first instead to say any of them (because
    > all the arrays have the same length), More clear my question now?


    Suppose so. Assuming that you by "length" mean the number of elements,
    this is one way:

    print scalar @{$myhash{(keys %myhash)[0]}};

    > On the future I will try to be more clear from the begining.


    Good. Also, in the future, please don't top post.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Sep 3, 2003
    #12
  13. Janek Schleicher

    John Bokma Guest

    Bigus wrote:

    > There is no "first value of a hash". That is, a hash just consists of keys
    > and values. If you want the lowest value in a hash, assuming the values are
    > all numbers of course, then you could do it something like this:
    >
    > @keys = sort {$hash{$a} <=> $hash{$b}} keys %hash;
    > print $hash{$keys[0]};


    technically this is O(n log n) while just looping over all keys and
    finding the lowest is O(n).

    my @keys = keys %hash;
    my $min = $hash{shift @keys}; # assign "first" as minimum
    foreach my $key (@keys) {
    $min = $hash{$key} if $hash{$key} < $min; # try to find better one
    }

    assuming %hash has values :)

    HTH

    --
    Kind regards, feel free to mail: mail(at)johnbokma.com (or reply)
    virtual home: http://johnbokma.com/ ICQ: 218175426
    John web site hints: http://johnbokma.com/websitedesign/
     
    John Bokma, Sep 3, 2003
    #13
  14. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    On Wed, 03 Sep 2003 15:13:54 +0000, Gunter Schelfhout wrote:

    > Gunter Schelfhout wrote:
    >
    >> On Wed, 03 Sep 2003 14:14:26 +0000, Gunter Schelfhout wrote:
    >>
    >>> Mina Naguib wrote:

    >> [snip...]
    >>>
    >>> Is this PGP-sig really nescessary on usenet? It's ugly like hell. And a
    >>> lot of work if you want to remove it on reply.

    >
    > blabla.
    >
    >> Yes, it is nescessary, it's the only way of telling who really sent the
    >> message. And, the signatures are roughly the acceptable size for usenet
    >> .sig's.

    >
    > blabla
    >
    >> - Brian Harnish ()

    >
    > I know where it is used for. IMHO, it doesn't make sense to use it on
    > usenet.
    > Look at the mess after only a couple of messages.
    > - --
    > Blood, sweat & tears


    Well, we're all entitled to oppinions. I don't think it's difficult to
    jump to the end of the message and delete a few lines when replying. And
    the only reason it got so ugly in our quoting is because we quoted the
    sigs, because thats what we were discussing. Ususally, you strip out any
    text that isn't relevant to the conversation (such as pgpsigs). If someone
    wants to verify that the quoted message came from the right person, they
    can check the original message.

    BTW: My news reader doesn't support pgp signing, so I copy/paste sign.
    Takes an extra couple of seconds, but I find it worthy, so that people
    know w/o a doubt that a message came from me.

    - Brian
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.2 (GNU/Linux)

    iD8DBQE/VhPRiK/rA3tCpFYRAte+AKCX4RXVkmbuKExeYURUeKxMnAdntACfWfMm
    bcxJnKy9QK4cRKQnUE86KeM=
    =E618
    -----END PGP SIGNATURE-----
     
    Brian Harnish, Sep 3, 2003
    #14
  15. Tina Mueller wrote:
    > Gunnar Hjalmarsson wrote:
    >>Assuming that you by "length" mean the number of elements,
    >>this is one way:
    >>
    >> print scalar @{$myhash{(keys %myhash)[0]}};

    >
    > or maybe one of these:
    > print scalar @{ (values %myhash)[0] };
    > print $#{ (values %myhash)[0] }+1;
    > print $#{ (each %myhash)[1] }+1;
    > print @{ (%myhash)[1] }+0;


    Yes. TMTOWTDI. But I have to admit that all your suggestions are more
    straight-forward than mine. I forgot about the values function. And
    each... Oh, well. ;-)

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Sep 3, 2003
    #15
  16. Brian Harnish wrote:

    >
    > Well, we're all entitled to oppinions.


    Certainly.

    > I don't think it's difficult to
    > jump to the end of the message and delete a few lines when replying.


    And on top.

    > And
    > the only reason it got so ugly in our quoting is because we quoted the
    > sigs, because thats what we were discussing. Ususally, you strip out any
    > text that isn't relevant to the conversation (such as pgpsigs). If someone
    > wants to verify that the quoted message came from the right person, they
    > can check the original message.


    OK. I don't have your public key. How should I check it? Via a key-server?
    You used my name, but not my headers. These should be enough to check the
    postings, no?

    > BTW: My news reader doesn't support pgp signing, so I copy/paste sign.
    > Takes an extra couple of seconds, but I find it worthy, so that people
    > know w/o a doubt that a message came from me.
    >


    Maybe you can put it after '-- ', so it will be cut by the readers which has
    the option set to cut signatures?

    Anyway, enough off-topic postings.
    --
    Blood, sweat & tears
     
    Gunter Schelfhout, Sep 3, 2003
    #16
  17. Janek Schleicher

    Anno Siegel Guest

    Tina Mueller <usenet@expires12.2003.tinita.de> wrote in comp.lang.perl.misc:
    > Gunnar Hjalmarsson wrote:
    > > Guest1 wrote:
    > >> I have a hash of arrays, and I would like to get the length of any
    > >> of these arrays. I said first instead to say any of them (because
    > >> all the arrays have the same length), More clear my question now?

    >
    > > Suppose so. Assuming that you by "length" mean the number of elements,
    > > this is one way:

    >
    > > print scalar @{$myhash{(keys %myhash)[0]}};

    >
    > or maybe one of these:
    > print scalar @{ (values %myhash)[0] };
    > print $#{ (values %myhash)[0] }+1;
    > print $#{ (each %myhash)[1] }+1;
    > print @{ (%myhash)[1] }+0;


    The "each"-solution is different from the others -- it doesn't expand
    all of the hash, which is good. It also leaves the hash with a not-
    quite-fresh iterator, which is bad. In a one-off program it probably
    doesn't matter, but in a general-purpose routine it would. Where is
    uneach?

    Anno
     
    Anno Siegel, Sep 3, 2003
    #17
  18. Janek Schleicher

    Uri Guttman Guest

    >>>>> "AS" == Anno Siegel <-berlin.de> writes:

    AS> The "each"-solution is different from the others -- it doesn't expand
    AS> all of the hash, which is good. It also leaves the hash with a not-
    AS> quite-fresh iterator, which is bad. In a one-off program it probably
    AS> doesn't matter, but in a general-purpose routine it would. Where is
    AS> uneach?

    it is called keys.

    uri

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
    Damian Conway Class in Boston - Sept 2003 -- http://www.stemsystems.com/class
     
    Uri Guttman, Sep 3, 2003
    #18
  19. Janek Schleicher

    Anno Siegel Guest

    Uri Guttman <> wrote in comp.lang.perl.misc:
    > >>>>> "AS" == Anno Siegel <-berlin.de> writes:

    >
    > AS> The "each"-solution is different from the others -- it doesn't expand
    > AS> all of the hash, which is good. It also leaves the hash with a not-
    > AS> quite-fresh iterator, which is bad. In a one-off program it probably
    > AS> doesn't matter, but in a general-purpose routine it would. Where is
    > AS> uneach?
    >
    > it is called keys.


    Yes and no. "keys" resets the iterator. The mythical uneach restores it,
    so the next call to each returns what it would have before. A tied hash
    could implement it.

    Anno
     
    Anno Siegel, Sep 3, 2003
    #19
  20. Janek Schleicher

    Keith Keller Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    On 2003-09-03, Uri Guttman <> wrote:
    >
    > it is called keys.


    In scalar context, will keys run through the whole hash? If so,
    then it doesn't sound the same as Anno's suggested ''uneach''.

    - --keith

    - --
    -francisco.ca.us
    (try just my userid to email me)
    AOLSFAQ=http://wombat.san-francisco.ca.us/cgi-bin/fom

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.0.6 (GNU/Linux)
    Comment: For info see http://www.gnupg.org

    iEYEARECAAYFAj9WVJYACgkQhVcNCxZ5ID9AQQCfUlN7rTA5utK0D2RJ/02BYHoi
    ZOQAn3WLbI9/fx8/cexin2qQu2tcrSJ9
    =ZXbe
    -----END PGP SIGNATURE-----
     
    Keith Keller, Sep 3, 2003
    #20
    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:
    562
    red floyd
    Nov 10, 2011
  2. David Garamond
    Replies:
    3
    Views:
    103
    Emmanuel Touzery
    Jan 14, 2004
  3. Une bévue
    Replies:
    5
    Views:
    165
    Une bévue
    Aug 10, 2006
  4. Farhad Farzaneh
    Replies:
    8
    Views:
    174
    Farhad Farzaneh
    Jul 5, 2007
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    655
    David A. Black
    Jul 2, 2008
Loading...

Share This Page