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