Danial Haris
Danial Haris

Reputation: 109

Sort Hash Value In A First Element Manner Perl

I have a hash that I want to sort its values in first found element manner. From below script, I sorted out the values in alphabetical manner, but what I really want is to sort it according to what values comes first, for example, key 0 with the values of A C B A D, the first value is A, so it sort all A first, then C, and then B and lastly D, I know how to sort it alphabetically but I can't figure out how to do with my wanted order.

#!/usr/bin/perl
use warnings;
use strict;
use List::MoreUtils;
use Tie::IxHash;    

my %KEY_VALUE;
#tie %KEY_VALUE,'Tie::IxHash';


my %KEY_VALUE= (
    0 => [ 'A', 'C', 'B', 'A' ,'D'],
    5 => [ 'D', 'F', 'E', 'F', 'F','E'],
    2 => [ 'Z', 'X', 'A', 'Y', 'X', 'Y', 'A' ],
    4 => [ 'E', 'R', 'M' ,'M','E'],
    3 => [ 'A', 'B', 'B', 'A' ],
    1 => [ 'C', 'C', 'F', 'E', 'C', 'E'],
    );

    #Code from Schwern user:14660
    # Iterate through the keys in numeric order.
    for my $key (sort {$a <=> $b } keys %KEY_VALUE)
    {
    # Get the value
    my $val = $KEY_VALUE{$key};

    # Sort it in place  #How to sort it in First Found Element instead of alphabetically increasing?

    @$val = sort { $a cmp $b } @$val;

    # Display it
    print "$key -> @$val\n";
    }

The wanted output is like this, and I want to print out the sorted keys and values.

    0 -> AACBD
    1 -> CCCFEE
    2 -> ZXXAAYY
    3 -> AABB
    4 -> EERMM
    5 -> DFFFEE

Upvotes: 1

Views: 146

Answers (2)

Nahuel Fouilleul
Nahuel Fouilleul

Reputation: 19315

If you view it as a sorting problem, you can build a mapping between the values and their desired order:

my @unsorted = qw( Z X X A Y X Y A );

my %order; @order{ reverse @unsorted } = reverse 0..$#unsorted;   # Z=>0, X=>1, A=>3, Y=>4
my @sorted = sort { $order{$a} <=> $order{$b} } @unsorted;        # ZXXXAAYY

But it's not actually sorting problem. Sorting is O(N log N), but this can be done in O(N).

my @unsorted = qw( Z X X A Y X Y A );

my %counts;
my @uniques = grep { !$counts{$_}++ } @unsorted;
my @sorted = map { ($_) x $counts{$_} } @uniques;                 # ZXXXAAYY

Upvotes: 2

Dave Cross
Dave Cross

Reputation: 69244

You get what I think you want, is pretty simple. You just walk the hash in sorted key order and then dereference the array and sort that.

#!/usr/bin/perl
use warnings;
use strict;

my %KEY_VALUE= (
    0 => [ 'A', 'C', 'B', 'A' ,'D'],
    5 => [ 'D', 'F', 'E', 'F', 'F','E'],
    2 => [ 'Z', 'X', 'A', 'Y', 'X', 'Y', 'A' ],
    4 => [ 'E', 'R', 'M' ,'M','E'],
    3 => [ 'A', 'B', 'B', 'A' ],
    1 => [ 'C', 'C', 'F', 'E', 'C', 'E'],
);

for my $key (sort { $a <=> $b } keys %KEY_VALUE) {
  print $key, " -> ", sort(@{ $KEY_VALUE{$key} }), "\n";
}

But that doesn't give the results you show. Please explain how each array should be sorted. I thought you wanted them sorted in alphabetical order, but that's not the case in your example.

I get this:

0 -> AABCD
1 -> CCCEEF
2 -> AAXXYYZ
3 -> AABB
4 -> EEMMR
5 -> DEEFFF

But you ask for:

0 -> AACBD
1 -> CCCFEE
2 -> ZXXAAYY
3 -> AABB
4 -> EERMM
5 -> DFFFEE

Upvotes: 0

Related Questions