Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Sorting by the value of the inner hash

by Anonymous Monk
on Mar 31, 2004 at 06:28 UTC ( #341219=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Monks
Could you please tell me why this code is not sorting my values?
$totalscoreinv{$key1}{key2}= $totalscore; foreach $keyscore (sort by_score keys %totalscoreinv){ print OUT "\n\n $keyscore\n"; foreach my $keyscore2 (keys %{$totalscoreinv{$keyscore}}){ printf(OUT "%30s %s\n", "$keyscore2= ", "$totalscoreinv{$keyscore} +{$keyscore2}"); } } sub by_score { $totalscorepass{$keyscore}{$b}<=>$totalscorepass{$keyscore}{$a}; }
One more question, once is sorted, how I can get/display/or compare with only the best(first) value by each outer-key?
Thanks

Replies are listed 'Best First'.
Re: Sorting by the value of the inner hash
by DamnDirtyApe (Curate) on Mar 31, 2004 at 07:26 UTC

    I'm going to suggest you do the following:

    • Provide a sample of real data you're feediing into this chunk of code
    • Provide the actual results you're getting
    • Provide the precise results you expected to get

    That aside, is it possible that $totalscoreinv{$key1}{key2} is supposed to be $totalscoreinv{$key1}{$key2}?


    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche
      Answering the third point, I expect the following
      First key:700200 523246= -438.024029240629 523247= -470.547762535975 523237= -494.393864137596 523261= -509.711586526694 523424= -521.488541326437

        That's great, but those points aren't mutually exclusive. We really need you to answer all of them to provide you with any useful information.

        TIP: Put use strict; at the beginning of this script, and every script you write from now on. You'll thank me one day.


        _______________
        DamnDirtyApe
        Those who know that they are profound strive for clarity. Those who
        would like to seem profound to the crowd strive for obscurity.
                    --Friedrich Nietzsche
Re: Sorting by the value of the inner hash
by halley (Prior) on Mar 31, 2004 at 14:58 UTC
    First, DamnedDirtyApe gave you a strong hint: the first line has a typo where you missed a dollar sign. This would put all the scores on a single key 'key2' in what you're calling the inner hash.

    Second, maybe this mistake was just typing your example wrong, but that's why we want you to know How to ask questions the smart way.

    Third, boil your test case down to a single routine which you CAN copy here in whole. This allows people to use their computers, not just their eyeballs, to help you find the problem. That includes a sample __DATA__ section if necessary. If we can't reproduce your problem at our desks, we are limited in how we can help.

    # build a data structure from <DATA> # use the data structure (print loops, etc.) # support the data structure (custom sort routines, etc.) __DATA__ # provide sample data

    --
    [ e d @ h a l l e y . c c ]

Re: Sorting by the value of the inner hash
by duff (Parson) on Mar 31, 2004 at 18:27 UTC

    Is $keyscore inside of the by_score subroutine the same as the one from your loop? At guess, I'd say it's not. Try putting the subroutine inline:

    foreach $keyscore (sort { $totalscoreinv{$keyscore}{$b}<=>$totalscorei +nv{$keyscore}{$a} } keys %totalscoreinv) { ... }
Re: Sorting by the value of the inner hash
by Anonymous Monk on Apr 01, 2004 at 02:57 UTC
    Thanks Monks it is working now, as you suggested I put the sort instruction inline.

    Could you please give me a hint for my second question? (original message)..Thanks
Re: Sorting by the value of the inner hash
by Anonymous Monk on Mar 31, 2004 at 06:34 UTC
    Sorry, I copy-paste the wrong subroutine
    sub by_score { $totalscoreinv{$keyscore}{$b}<=>$totalscoreinv{$keyscore}{$a}; }
      Well, without your either:
      1. Posting the whole relevant section of your new code or
      2. Telling us exactly how it's not working
      It's hard for any of us to help you.
        1. Modified code
        $totalscoreinv{$key1}{key2}= $totalscore; foreach $keyscore (keys %totalscoreinv){ print OUT "\n\nFirst key:$keyscore\n"; foreach my $keyscore2 (sort by_score keys %{$totalscoreinv{$keyscore +}}){ printf(OUT "%30s %s\n", "$keyscore2= ", "$totalscoreinv{$keyscore} +{$keyscore2}"); } } sub by_score { $totalscoreinv{$keyscore}{$b}<=>$totalscoreinv{$keyscore}{$a}; }
        2. well..as simple as it is not sorting at all. Here an excerpt of my results
        First key:700200 ----------------------------------------------------------- 523261= -509.711586526694 523237= -494.393864137596 523246= -438.024029240629 523424= -521.488541326437 523247= -470.547762535975
        Thanks
        I moved it to the inner loop, and still it is not working. Thanks

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://341219]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2021-12-01 07:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?