Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: Sorting and ranking

by tybalt89 (Monsignor)
on Jan 21, 2018 at 16:33 UTC ( [id://1207635]=note: print w/replies, xml ) Need Help??


in reply to Sorting and ranking

And now for something completely? different...

#!/usr/bin/perl # http://perlmonks.org/?node_id=1207623 use strict; use warnings; my %data = ( 'car' => 180, 'motorcycle' => 150, 'skate' => 150, 'bird' => 120, ); my ($n, @rank) = 1; $rank[ $data{$_} ] .= "$_\n" for sort keys %data; defined and $n += print s/^/$n - /gmr for reverse @rank;

Outputs:

1 - car 2 - motorcycle 2 - skate 3 - bird

Replies are listed 'Best First'.
Re^2: Sorting and ranking
by Lotus1 (Vicar) on Jan 21, 2018 at 21:09 UTC

    This made more sense to me once I had a look at the array contents. The rank array stores the key names at the index locations corresponding to the values.

    use strict; use warnings; use Data::Dumper; my %data = ( 'car' => 10, 'motorcycle' => 7, 'skate' => 7, 'board' => 2, 'roller' => 4, 'vase' => 4, 'bird' => 4, ); my ($n, @rank) = 1; foreach( keys %data){ $rank[ $data{$_} ] .= "$_\n" ; } print Dumper(\@rank); defined and $n += print s/^/$n - /gmr for reverse @rank; __DATA__ $VAR1 = [ undef, undef, 'board ', undef, 'vase bird roller ', undef, undef, 'motorcycle skate ', undef, undef, 'car ' ]; 1 - car 2 - motorcycle 2 - skate 3 - vase 3 - bird 3 - roller 4 - board

      You've discovered my secret. Oh, wait, it's not a secret, it's a distribution sort.

      To see a hardware implementation of a distribution sort, check out IBM card sorter.

Re^2: Sorting and ranking
by haukex (Archbishop) on Jan 22, 2018 at 15:27 UTC
    $rank[ $data{$_} ] .= "$_\n" for sort keys %data; defined and $n += print s/^/$n - /gmr for reverse @rank;

    Clever as always :-) It should be noted, however, that memory usage (and speed) gets worse for increasingly large values in the hash, since it causes @rank to grow accordingly.

      Of course. One of the beauties of perl, however, is the ability to easily switch over to using a hash. The sort will have to be explicit, of course. instead of getting it as a freebie with the array.

      As a bonus, you get the ability to properly handle negative and/or non-integer values.

      #!/usr/bin/perl # http://perlmonks.org/?node_id=1207623 use strict; use warnings; my %data = ( 'car' => 180, 'motorcycle' => 150, 'skate' => 150, 'bird' => 120, ); my ($n, %rank) = 1; $rank{ $data{$_} + 0 } .= "$_\n" for sort keys %data; $n += print s/^/$n - /gmr for @rank{ sort { $b <=> $a } keys %rank };

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-23 20:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found