how to sort a hash of arrays?

Discussion in 'Perl Misc' started by Jerry Preston, Oct 25, 2004.

  1. I know the following works:

    if( @{ $dat{ $ST }} ) {
    @dat_ = @{ $dat{ $ST }};
    foreach $j ( @dat ) {
    ....
    }
    }
    I am trying to figure out how to do in the following format of which do not
    work:

    foreach $j ( sort { @{ $wafers{ $ST }{ $a } } cmp @{ $wafers{ $ST }{
    $b } } } @{ $wafers{ $ST }} ) {
    or
    foreach $j ( sort { @{ $wafers{ $a } } cmp @{ $wafers{ $b } } } keys
    %wafers ) {
    or
    if( @{ $dat{ $ST }} ) {
    $j= [ sort { $a cmp $b } @{ $j } ];

    Any ideas?

    Thanks,

    Jerry
     
    Jerry Preston, Oct 25, 2004
    #1
    1. Advertising

  2. Jerry Preston

    Anno Siegel Guest

    Jerry Preston <> wrote in comp.lang.perl.misc:
    > I know the following works:
    >
    > if( @{ $dat{ $ST }} ) {
    > @dat_ = @{ $dat{ $ST }};
    > foreach $j ( @dat ) {
    > ....
    > }
    > }


    Works? Whatever you mean with that, you are using "dat" once as a
    hash and once as an array. Your array is "@dat_", so no, it won't
    even compile.

    > I am trying to figure out how to do in the following format of which do not
    > work:


    "Does not work" is no valid error description, especially since
    you have never said what "work" would mean.

    > foreach $j ( sort { @{ $wafers{ $ST }{ $a } } cmp @{ $wafers{ $ST }{
    > $b } } } @{ $wafers{ $ST }} ) {
    > or
    > foreach $j ( sort { @{ $wafers{ $a } } cmp @{ $wafers{ $b } } } keys
    > %wafers ) {
    > or
    > if( @{ $dat{ $ST }} ) {
    > $j= [ sort { $a cmp $b } @{ $j } ];
    >
    > Any ideas?


    No. Say what you want to achieve and we may have suggestions.

    Throwing bits of code around without saying what they are supposed
    to do is just as bad as a verbal problem description without any code.

    Anno
     
    Anno Siegel, Oct 25, 2004
    #2
    1. Advertising

  3. Jerry Preston wrote:
    > I know the following works:


    By some private definition of "works" which you didn't share with us.

    [code snipped]
    > I am trying to figure out how to do in the following format of which
    > do not work:


    By some private definition of "do not work" which you didn't share with us.

    [code snipped]
    > Any ideas?


    Yes. Please let us know what you expect the code to do ("it works") versus
    what you are actually observing ("does not work"). We are not mind readers,
    therefore we cannot know what your were thinking when you wrote "works"
    (although some people are close).

    As for "works" in combinatino with your subject line: this is akin to a
    perpetuum mobile. A hash by definition does not have a (useful) order,
    therefore you cannot sort it. You can probably sort the keys or sort the
    values, but there is no way to sort the hash itself.

    There is the Tie::IxHash module, which emulates order on hashes. Please see
    Google about earlier postings on this subject.

    Oh, and before I forget: this _Q_uestion is _A_sked _F_requenty. Please
    check the FAQ (perldoc -q hash) beforehand next time:
    How do I sort a hash (optionally by value instead of key)?
    How can I always keep my hash sorted?


    jue
     
    Jürgen Exner, Oct 25, 2004
    #3
    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. Navin
    Replies:
    1
    Views:
    748
    Ken Schaefer
    Sep 9, 2003
  2. rp
    Replies:
    1
    Views:
    562
    red floyd
    Nov 10, 2011
  3. Mmcolli00 Mom
    Replies:
    5
    Views:
    145
    Mmcolli00 Mom
    Jan 12, 2009
  4. Tore Aursand
    Replies:
    3
    Views:
    570
    Anno Siegel
    Sep 16, 2003
  5. use hash of arrays to sort

    , Sep 12, 2005, in forum: Perl Misc
    Replies:
    6
    Views:
    121
    William James
    Sep 14, 2005
Loading...

Share This Page