Hash Sorting

Discussion in 'Perl Misc' started by Brad, Jun 14, 2005.

  1. Brad

    Brad Guest

    I have a hash like this:

    $HASH{'key'}{'date'} = some date
    $HASH{'key'}{'status'} = some status

    I'd like to sort and display this by date:

    key | date | status

    I cannot seem to find any references to sorting this type of hash in this
    way. I need some assistance finding some documentation, or a copy/paste of
    the code for this type of sort.

    Thanks for any help,
    Brad
     
    Brad, Jun 14, 2005
    #1
    1. Advertising

  2. On Tue, 14 Jun 2005 13:49:56 -0700, Brad wrote:

    > I have a hash like this:
    >
    > $HASH{'key'}{'date'} = some date
    > $HASH{'key'}{'status'} = some status
    >
    > I'd like to sort and display this by date:
    >
    > key | date | status
    >
    > I cannot seem to find any references to sorting this type of hash in this
    > way. I need some assistance finding some documentation, or a copy/paste of
    > the code for this type of sort.
    >

    You can do it with a double map and sort.

    Google for

    "schwarzian transform" perl

    Mark
     
    Mark Clements, Jun 14, 2005
    #2
    1. Advertising

  3. Brad

    Brad Guest

    > You can do it with a double map and sort.
    >
    > Google for
    >
    > "schwarzian transform" perl
    >
    > Mark


    Thanks Mark... kept searching, after my post, and I found the answer:

    foreach my $spc ( sort { $hoh{$a}->{'cpu'} <=> $hoh{$b}->{'cpu'} } keys
    %hoh) {
    print $spc, " has got ", ${hoh}{$spc}{cpu}, "\n";
    }
     
    Brad, Jun 14, 2005
    #3
  4. Brad

    Anno Siegel Guest

    Mark Clements <> wrote in comp.lang.perl.misc:
    > On Tue, 14 Jun 2005 13:49:56 -0700, Brad wrote:
    >
    > > I have a hash like this:
    > >
    > > $HASH{'key'}{'date'} = some date
    > > $HASH{'key'}{'status'} = some status
    > >
    > > I'd like to sort and display this by date:
    > >
    > > key | date | status
    > >
    > > I cannot seem to find any references to sorting this type of hash in this
    > > way. I need some assistance finding some documentation, or a copy/paste of
    > > the code for this type of sort.
    > >

    > You can do it with a double map and sort.
    >
    > Google for
    >
    > "schwarzian transform" perl


    Why do you think that's useful?

    A Schwartzian transform is an efficiency hack that saves time when the
    extraction of sort keys from the elements is expensive. It doesn't
    facilitate any particular ordering, you'll have to do that either way.

    Since the sort keys are available through hash access, there isn't much
    a Schwartzian can gain in this case.

    Anno
     
    Anno Siegel, Jun 15, 2005
    #4
  5. On 15 Jun 2005 09:40:46 GMT, Anno Siegel wrote:

    > Mark Clements <> wrote in comp.lang.perl.misc:
    >> On Tue, 14 Jun 2005 13:49:56 -0700, Brad wrote:
    >>
    >>> I have a hash like this:
    >>>
    >>> $HASH{'key'}{'date'} = some date
    >>> $HASH{'key'}{'status'} = some status
    >>>
    >>> I'd like to sort and display this by date:
    >>>
    >>> key | date | status
    >>>
    >>> I cannot seem to find any references to sorting this type of hash in this
    >>> way. I need some assistance finding some documentation, or a copy/paste of
    >>> the code for this type of sort.
    >>>

    >> You can do it with a double map and sort.
    >>
    >> Google for
    >>
    >> "schwarzian transform" perl

    >
    > Why do you think that's useful?

    It demonstrates passing different comparison operations to sort, and the
    use of multiple maps, though I didn't realise that the Schwarzian transform
    was specific to non-prekeyed data.

    >
    > A Schwartzian transform is an efficiency hack that saves time when the
    > extraction of sort keys from the elements is expensive. It doesn't
    > facilitate any particular ordering, you'll have to do that either way.


    > Since the sort keys are available through hash access, there isn't much
    > a Schwartzian can gain in this case.

    Fair enough, but the suggestion was intended more as a learning exercise
    than as a "do it this way".

    Mark
     
    Mark Clements, Jun 15, 2005
    #5
    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. Replies:
    2
    Views:
    1,441
    James Kanze
    Jul 6, 2010
  2. rp
    Replies:
    1
    Views:
    540
    red floyd
    Nov 10, 2011
  3. Williams, Chris
    Replies:
    3
    Views:
    109
    Florian Gross
    Dec 13, 2004
  4. Srijayanth Sridhar
    Replies:
    19
    Views:
    627
    David A. Black
    Jul 2, 2008
  5. IanW
    Replies:
    3
    Views:
    127
    Ian Stuart
    Dec 14, 2005
Loading...

Share This Page