[ create a new paste ] login | about

Link: http://codepad.org/DJpRS7Vj    [ raw code | output | fork ]

vol7ron - Perl, pasted on Nov 29:
    my %hash = (a=>1, b=>3, c=>3, d=>2, e=>1, f=>1);
    my %hash_by_val;
    
    for my $key ( sort { $hash{ $a } <=> $hash{ $b } 
                       } keys %hash  
                ) { 
       push @{$hash_by_val{$hash{$key}}}, $key;
    }
    
    
    for my $key (sort keys %hash_by_val){
       my @arr        = @{$hash_by_val{$key}};
       my $arr_length = $#arr;

       for (0..$arr_length){
          my $randnum = int(rand($arr_length));
          my $val     = splice (@arr,$randnum,1);

          $arr_length--;
          print "$key : $val\n";                    # notice: output varies b/t runs
       }
    }


Output:
1
2
3
4
5
6
1 : a
1 : e
1 : f
2 : d
3 : c
3 : b


Create a new paste based on this one


Comments: