fork(1) download
  1. #!/usr/bin/perl -w
  2.  
  3. use strict;
  4.  
  5. my %tmp = (
  6. "key1" => 3,
  7. "key2" => 6,
  8. "key3" => 1,
  9. "key4" => 3,
  10. );
  11.  
  12. my %sorted_by_values = ();
  13.  
  14. for my $key ( keys %tmp ) {
  15. my $value = $tmp{ $key };
  16.  
  17. if ( exists $sorted_by_values{ $value } ) {
  18. push @{ $sorted_by_values{ $value } }, $key;
  19. } else {
  20. $sorted_by_values{ $value } = [ $key ];
  21. }
  22. }
  23.  
  24. for my $value ( sort keys %sorted_by_values ) {
  25. for my $key ( sort @{ $sorted_by_values{ $value } } ) {
  26. printf "%s => %d\n", $key, $value;
  27. }
  28. }
Success #stdin #stdout 0s 6044KB
stdin
Standard input is empty
stdout
key3 => 1
key1 => 3
key4 => 3
key2 => 6