<-----<<<[/ref][/ref][/ref] Chris Mortimore Information Services Graceland University cmortimr at graceland dot edu [allowsmilie] => 1 [showsignature] => 0 [ipaddress] => [iconid] => 0 [visible] => 1 [attach] => 0 [infraction] => 0 [reportthreadid] => 0 [isusenetpost] => 1 [msgid] => [ref] => [htmlstate] => on_nl2br [postusername] => Chris [ip] => CMORTIMR@gracel [isdeleted] => 0 [usergroupid] => [membergroupids] => [displaygroupid] => [password] => [passworddate] => [email] => [styleid] => [parentemail] => [homepage] => [icq] => [aim] => [yahoo] => [msn] => [skype] => [showvbcode] => [showbirthday] => [usertitle] => [customtitle] => [joindate] => [daysprune] => [lastvisit] => [lastactivity] => [lastpost] => [lastpostid] => [posts] => [reputation] => [reputationlevelid] => [timezoneoffset] => [pmpopup] => [avatarid] => [avatarrevision] => [profilepicrevision] => [sigpicrevision] => [options] => [akvbghsfs_optionsfield] => [birthday] => [birthday_search] => [maxposts] => [startofweek] => [referrerid] => [languageid] => [emailstamp] => [threadedmode] => [autosubscribe] => [pmtotal] => [pmunread] => [salt] => [ipoints] => [infractions] => [warnings] => [infractiongroupids] => [infractiongroupid] => [adminoptions] => [profilevisits] => [friendcount] => [friendreqcount] => [vmunreadcount] => [vmmoderatedcount] => [socgroupinvitecount] => [socgroupreqcount] => [pcunreadcount] => [pcmoderatedcount] => [gmmoderatedcount] => [assetposthash] => [fbuserid] => [fbjoindate] => [fbname] => [logintype] => [fbaccesstoken] => [newrepcount] => [vbseo_likes_in] => [vbseo_likes_out] => [vbseo_likes_unread] => [temp] => [field1] => [field2] => [field3] => [field4] => [field5] => [subfolders] => [pmfolders] => [buddylist] => [ignorelist] => [signature] => [searchprefs] => [rank] => [icontitle] => [iconpath] => [avatarpath] => [hascustomavatar] => 0 [avatardateline] => [avwidth] => [avheight] => [edit_userid] => [edit_username] => [edit_dateline] => [edit_reason] => [hashistory] => [pagetext_html] => [hasimages] => [signatureparsed] => [sighasimages] => [sigpic] => [sigpicdateline] => [sigpicwidth] => [sigpicheight] => [postcount] => 1 [islastshown] => [isfirstshown] => 1 [attachments] => [allattachments] => ) --> Sorting an array of hashes - PERL Beginners

Sorting an array of hashes - PERL Beginners

I want to sort an AoH. Not each hash by its keys, but the array by the value of one of the keys in each hash. I know how to sort a simple array. I know how to sort a hash by the keys. Could someone kindly point me to the doentation on sorting arrays of hashes? Thank you! Chris.  [/ref][/ref] Chris Mortimore Information Services Graceland University cmortimr at graceland dot edu...

  1. #1

    Default Sorting an array of hashes

    I want to sort an AoH. Not each hash by its keys, but the array by the
    value of one of the keys in each hash.
    I know how to sort a simple array.
    I know how to sort a hash by the keys.
    Could someone kindly point me to the doentation on sorting arrays of
    hashes?

    Thank you!
    Chris.
     [/ref][/ref]
    Chris Mortimore
    Information Services
    Graceland University
    cmortimr at graceland dot edu


    Chris Guest

  2. #2

    Default Re: Sorting an array of hashes

    Chris Mortimore wrote: 

    The value of one of the keys? If you don't know *which* key in
    respective hash, this appears to be pretty tricky...

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Guest

  3. #3

    Default RE: Sorting an array of hashes

    Chris Mortimore wrote: 

    The value of one of the keys? If you don't know *which* key in
    respective hash, this appears to be pretty tricky...

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl

    --

    Of course I know _which_ key. Each hash has a key "date_tm", I want to
    sort all the hashes in the array by their date_tm value which is in the
    format: yyyymmdddhhmm.

    Chris.
    Chris Guest

  4. #4

    Default RE: Sorting an array of hashes

    On Thu, 5 Aug 2004, Chris Mortimore wrote:
     
    > >
    > > The value of one of the keys? If you don't know *which* key in
    > > respective hash, this appears to be pretty tricky...[/ref]
    >
    > Of course I know _which_ key. Each hash has a key "date_tm", I want
    > to sort all the hashes in the array by their date_tm value which is in
    > the format: yyyymmdddhhmm.[/ref]

    Don't you think it might have been constructive to mention this the
    first time around?

    Can you please give a fuller description of [a] what this data structure
    really looks like -- a Data::Dumper dump would be good -- and [b] how
    you want to rearrange the contents of this structure?


    --
    Chris Devers
    Chris Guest

  5. #5

    Default RE: Sorting an array of hashes

    -----Original Message-----
    From: Chris Mortimore [mailto:edu]
    Sent: Thursday, August 05, 2004 5:19 PM
    To: org
    Subject: Sorting an array of hashes

    I want to sort an AoH. Not each hash by its keys, but the array by the
    value of one of the keys in each hash.
    I know how to sort a simple array.
    I know how to sort a hash by the keys.
    Could someone kindly point me to the doentation on sorting arrays of
    hashes?

    Thank you!
    Chris.

    Hope this gives you some ideas...


    #! /usr/local/bin/perl
    AoH=({office=>C,employees=>500},{office=>A, employees=>30});
    $ndx=0;
    foreach (AoH){
    print "$_->{office}, $ndx \n";
    $IofH{$_->{office}} = $ndx++;
    }
    foreach (sort keys %IofH){
    print "$_, $IofH{$_}\n";
    %nH = %{$AoH[$IofH{$_}]};
    print "Office $_ has employees $nH{employees}\n";
    }

    jwm
    John Guest

  6. #6

    Default Re: Sorting an array of hashes

    On 8/5/2004 5:18 PM, Chris Mortimore wrote: 

    `perldoc -q sort`

    Ex.

    my AoH = ...
    my sorted = sort { $a->{key} cmp $b->{key} } AoH;

    Randy Guest

  7. #7

    Default Re: Sorting an array of hashes

    Chris Mortimore wrote: 
    >>
    >> The value of one of the keys? If you don't know *which* key in
    >> respective hash, this appears to be pretty tricky...[/ref]
    >
    > Of course I know _which_ key. Each hash has a key "date_tm", I
    > want to sort all the hashes in the array by their date_tm value
    > which is in the format: yyyymmdddhhmm.[/ref]

    Aha, the keys have the same name.. Good! Then Randy's suggested code
    should do.

    As regards doentation, besides "perldoc -f sort", there is a FAQ
    entry that is very much applicable to this problem: "How do I sort an
    array by (anything)?"

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Guest

  8. #8

    Default RE: Sorting an array of hashes


    -----Original Message-----
    From: Chris Mortimore [mailto:edu]
    Sent: Thursday, August 05, 2004 5:19 PM
    To: org
    Subject: Sorting an array of hashes

    I want to sort an AoH. Not each hash by its keys, but the array by the
    value of one of the keys in each hash. I know how to sort a simple
    array. I know how to sort a hash by the keys. Could someone kindly point
    me to the doentation on sorting arrays of hashes?

    Thank you!
    Chris.

    Hope this gives you some ideas...


    #! /usr/local/bin/perl AoH=({office=>C,employees=>500},{office=>A,
    employees=>30}); $ndx=0; foreach (AoH){
    print "$_->{office}, $ndx \n";
    $IofH{$_->{office}} = $ndx++;
    }
    foreach (sort keys %IofH){
    print "$_, $IofH{$_}\n";
    %nH = %{$AoH[$IofH{$_}]};
    print "Office $_ has employees $nH{employees}\n";
    }

    jwm


    Thank you John. How to build the index and relate it back to the
    original array of hashes is exactly what I was trying to figure out!
    Gratefully, Chris.
    Chris Guest

Similar Threads

  1. Sorting array vs sorting paginated array
    By limitedwave777 in forum Macromedia Flex General Discussion
    Replies: 0
    Last Post: May 14th, 01:58 PM
  2. Sorting Hash of Hashes with HEAP module
    By Ilja in forum PERL Modules
    Replies: 0
    Last Post: March 17th, 09:56 PM
  3. Help with array, hashes and objects
    By George in forum PERL Beginners
    Replies: 2
    Last Post: February 18th, 11:29 AM
  4. Slice an array of hashes?
    By Graham in forum PERL Miscellaneous
    Replies: 6
    Last Post: September 11th, 07:20 PM
  5. Sorting an array of hashes by 2 values
    By Paul Tomlinson in forum PERL Miscellaneous
    Replies: 4
    Last Post: September 2nd, 01:50 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139