Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Trying to make a string generated like google

by davorg (Chancellor)
on Jul 20, 2009 at 10:39 UTC ( #781592=note: print w/ replies, xml ) Need Help??


in reply to Trying to make a string generated like google

my @sorted_phases = sort { $b->{count} <=> $a->{count} } values %$strings;

You'll then have an array of your hashes ordered by the "count" value. The ones with the highest count will be at the start of the array.

--

See the Copyright notice on my home node.

Perl training courses


Comment on Re: Trying to make a string generated like google
Download Code
Re^2: Trying to make a string generated like google
by ultranerds (Pilgrim) on Jul 20, 2009 at 10:43 UTC
    OMG - your a star! I've been trying to get something like that to work for ages, and you did it in one line - thanks, it doesn't *exactly* what I need :)

    Cheers

    Andy
Re^2: Trying to make a string generated like google
by ultranerds (Pilgrim) on Aug 10, 2009 at 14:01 UTC
    Hi,

    I'm trying to do something a bit more complex now:

    my @sorted_phrases = sort { $b->{sort_number} <=> $a->{sort_number +}, $b->{count} <=> $a->{count} } values %$strings;


    ..instead of just:

    my @sorted_phrases = sort { $b->{count} <=> $a->{count} } values %$strings;


    Basically, what I need to do - is :

    1) Sort by the value "count" as they were done before
    2) If the 2nd array element has a value of "0" for "count", then it will order them by sort_number thereafter.

    I've tried all kinda things - including this messy bit of code - but I can't seem to get it working :(

    my @sorted_phrases_2; # my $i = 0; foreach (my $i = 0; $i <= $#sorted_phrases; $i++) { local $_ = $sorted_phrases[$i]; if ($i == 1) { print "BLA BLA XXX - \"$_->{count}\" " . Dumper($_); if ($_->{count} < 1) { my $chosen_id_now = $sorted_phrases[0]->{sort_number} ++ 1; print qq|<br />Current sort was: $sorted_phrases[0]->{ +sort_number} , and new one is $chosen_id_now <br />|; foreach my $tmp (@sorted_phrases) { if ($tmp->{sort_number} == $chosen_id_now) { push @sorted_phrases_2, $tmp; } } } } else { push @sorted_phrases_2, $_; } } @sorted_phrases = @sorted_phrases_2;


    Thanks for any ideas - as I'm stumped :(

    Cheers

    Andy

      I've just logged on for the first time for almost a year and saw your unanswered question. It's really quite simple.

      my @sorted_phrases = sort { $b->{count} <=> $a->{count} or $b->{sort_number} <=> $a->{sort_number} } values %$strings;

      It's really worth reading the Perl FAQ. See How do I sort an array by (anything)?

      --

      See the Copyright notice on my home node.

      Perl training courses

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2014-09-02 02:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (18 votes), past polls