Printing a hash question

Discussion in 'Perl Misc' started by scadav, Jun 25, 2004.

  1. scadav

    scadav Guest

    I am new to Perl and this problem is baffling me. I undestand how to
    print out all the items in a hash and sort by the keys:

    foreach $item (sort keys(%hash)) {
    print "$item: $hash{$item} \n";
    }

    but how do you do the reverse of this?...print out all of the items
    but sort on the values?

    any help would be appreciated.
     
    scadav, Jun 25, 2004
    #1
    1. Advertising

  2. scadav wrote:
    > I am new to Perl and this problem is baffling me. I undestand how
    > to print out all the items in a hash and sort by the keys:
    >
    > foreach $item (sort keys(%hash)) {
    > print "$item: $hash{$item} \n";
    > }
    >
    > but how do you do the reverse of this?...print out all of the items
    > but sort on the values?


    That's a Frequently Asked Question:

    perldoc -q "sort a hash"

    You are supposed to check the Perl FAQ before posting questions here.

    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Jun 25, 2004
    #2
    1. Advertising

  3. scadav

    John Bokma Guest

    scadav wrote:

    > I am new to Perl and this problem is baffling me. I undestand how to
    > print out all the items in a hash and sort by the keys:
    >
    > foreach $item (sort keys(%hash)) {
    > print "$item: $hash{$item} \n";
    > }
    >
    > but how do you do the reverse of this?...print out all of the items
    > but sort on the values?



    for my $key ( sort { $hash{$a} cmp $hash{$b} } keys %hash ) {

    }

    --
    John MexIT: http://johnbokma.com/mexit/
    personal page: http://johnbokma.com/
    Experienced Perl programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html
     
    John Bokma, Jun 25, 2004
    #3
  4. scadav <> wrote:

    > print out all of the items
    > but sort on the values?
    >
    > any help would be appreciated.



    Any checking of the Perl FAQ *before* posting to the
    Perl newsgroup appreciated.


    perldoc -q hash
    perldoc -q sort


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jun 26, 2004
    #4
  5. scadav

    Eric Bohlman Guest

    John Bokma <> wrote in
    news:40dcad6a$0$873$:

    >
    >
    > scadav wrote:
    >
    >> I am new to Perl and this problem is baffling me. I undestand how to
    >> print out all the items in a hash and sort by the keys:
    >>
    >> foreach $item (sort keys(%hash)) {
    >> print "$item: $hash{$item} \n";
    >> }
    >>
    >> but how do you do the reverse of this?...print out all of the items
    >> but sort on the values?

    >
    >
    > for my $key ( sort { $hash{$a} cmp $hash{$b} } keys %hash ) {
    >
    > }


    It's a bad idea to try to answer Frequently Asked Questions yourself rather
    than referring the poster to the FAQ. IIRC, last week a well-known regular
    (I forget who) let his guard down and tried to answer a FAQ himself, but in
    the process he made a mistake. Another regular posted a correction, but
    it's quite possible that the original poster, not to mention any number of
    lurkers, stopped reading the thread as soon as they saw what looked like an
    answer and missed the correction (the correction may not have made it to
    their news server yet).

    Someone who asks FAQs is, almost by definition, someone who doesn't yet
    know enough about Perl to be able to tell whether or not he's getting a bum
    steer. There's very little chance that the Perl FAQ will give them a bum
    steer. There's a much greater chance that a Usenet poster will
    accidentally do so.
     
    Eric Bohlman, Jun 27, 2004
    #5
  6. scadav

    John Bokma Guest

    Eric Bohlman wrote:

    > John Bokma <> wrote in


    >>for my $key ( sort { $hash{$a} cmp $hash{$b} } keys %hash ) {
    >>
    >>}


    > It's a bad idea to try to answer Frequently Asked Questions yourself rather


    Sometimes postings like the above are added to the FAQ, or can be a
    correction on the FAQ.

    OTOH I agree with you too ;-)

    > the process he made a mistake. Another regular posted a correction, but


    No problem there. I see often corrections and/or additions by people
    here, wether it are FAQ questions or not. It keeps one sharp, and one
    learns.

    > it's quite possible that the original poster, not to mention any number of
    > lurkers, stopped reading the thread as soon as they saw what looked like an
    > answer and missed the correction (the correction may not have made it to
    > their news server yet).


    So they have a non-working solution. Either they correct it themself and
    learn from it or they come back for more.

    You mistake Usenet with a free helpdesk and solution providing thingy.

    > Someone who asks FAQs is, almost by definition, someone who doesn't yet
    > know enough about Perl to be able to tell whether or not he's getting a bum
    > steer. There's very little chance that the Perl FAQ will give them a bum
    > steer.


    Someone who just copy-pastes code will run into many problems. Wether
    the FAQ is the source or not. The FAQ will rarely provide what they
    exactly want.

    > There's a much greater chance that a Usenet poster will
    > accidentally do so.


    And nothing wrong there. The answers are given for free.

    I see that there is a sorting question ;-)

    --
    John MexIT: http://johnbokma.com/mexit/
    personal page: http://johnbokma.com/
    Experienced Perl programmer available: http://castleamber.com/
    Happy Customers: http://castleamber.com/testimonials.html
     
    John Bokma, Jun 27, 2004
    #6
    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. Jon B
    Replies:
    7
    Views:
    7,562
    =?Utf-8?B?SmFzb25DaG9p?=
    Jan 30, 2006
  2. rp
    Replies:
    1
    Views:
    556
    red floyd
    Nov 10, 2011
  3. Ronald Fischer

    Hash#keys, Hash#values order question

    Ronald Fischer, Aug 23, 2007, in forum: Ruby
    Replies:
    0
    Views:
    163
    Ronald Fischer
    Aug 23, 2007
  4. Srijayanth Sridhar
    Replies:
    19
    Views:
    640
    David A. Black
    Jul 2, 2008
  5. Älphä Blüë

    Hash of Hash of Arrays Question

    Älphä Blüë, Jul 18, 2009, in forum: Ruby
    Replies:
    5
    Views:
    676
    Älphä Blüë
    Jul 18, 2009
Loading...

Share This Page