in reply to How can I get the four highest valued letters in an array?
Hello supriyoch_2008,
Another possible way, using Slices.
I would have the same approach as 1nickt did already. Initially create a hash from two arrays and then simply sort the values in descending order.
#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %hash; my @x = qw/d b c e a f/; my @y = qw/4 6 5 2 9 1/; @hash{@x} = @y; print Dumper \%hash; my @sorted = (sort {$hash{$b} <=> $hash{$a}} keys %hash) [0 .. 3]; print Dumper \@sorted; __END__ $ perl test.pl $VAR1 = { 'f' => '1', 'c' => '5', 'a' => '9', 'd' => '4', 'b' => '6', 'e' => '2' }; $VAR1 = [ 'a', 'b', 'c', 'd' ];
Hope this helps.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: How can I get the four highest valued letters in an array?
by supriyoch_2008 (Monk) on Jun 01, 2017 at 14:49 UTC | |
by thanos1983 (Parson) on Jun 01, 2017 at 16:24 UTC |