Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

sort a hash array

by fuzzysteve (Beadle)
on Nov 18, 2001 at 07:35 UTC ( [id://126080]=CUFP: print w/replies, xml ) Need Help??

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: sort a hash array
by wog (Curate) on Nov 18, 2001 at 07:57 UTC
    my @array1 = sort { $queryterms{$b} <=> $queryterm{$a} || $a cmp $b } keys %queryterms; my @array2 = map { $queryterms{$_} } @array1;

    Smaller. Faster. Alternately one could just do my @sortedkeys = sort { $queryterms{$b} <=> $queryterm{$a} || $a cmp $b } keys %queryterms; and then use @queryterm{@sortkeys} instead of @array1 and @sortedkeys (better named, IMO) instead of @array1.

    update: added key comparing to sort, like the original had.

    (update: I would note that if you can't use perl's built-in sort, you probably should use a better algorithm. Yours takes O(N**2) comparisions where as perl's sort algorithm (and most of the good sorting algortihms) average O(N log N) comparisions. )

    (I forewarn that I have not tested the code here.)

      I bow to your superiour knowledge.
      In my own defence (or probably damnation) Most of the code is a quick hack I threw together to solve a problem at hand. It worked, so I didn't tweak it. Looking back, I should have.
      As an aside, I didn't pick the sort routine as for its speed, I picked it for its ease of coding. this is lifted from some code thats going to be run nightly, at a time when the servers going to be unused. in a test on sample data, it doesn't take too long to work, so is suitable for the job.
      In the end, I'm not arguing with you. Its not pretty, its not fast. It's also not a good use of the functions perl has available. but it does work, and its simple to understand.
      Thanks for the code btw, as its here, I should probably try it
Re: sort a hash array
by Jazz (Curate) on Nov 18, 2001 at 10:34 UTC
      thanks, I'll take a look
(jeffa) Re: sort a hash array
by jeffa (Bishop) on Nov 18, 2001 at 21:22 UTC
    Instead of using 'C-style' for loops:
    my $index=0; for ($index=0;$index<=$x;$index++) { }
    Compel to the power of Perl:
    for my $index(0..$x) { }
    Also, i have found that i if i need to sort a hash, then i probably should have been using an array instead. There is a reason why one shouldn't care about the order of hash keys. BUT, when you do care, sort is your friend.

    And personally, i find wog's code easier to understand than yours. Just give it some time and you will too. ;)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    F--F--F--F--F--F--F--F--
    (the triplet paradiddle)
    

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (6)
As of 2024-04-19 12:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found