Thread Sort Array of Hashes by Key (7 answers)
Opened by devrand at 2012-09-18 21:12

devrand
 2012-09-28 21:14
#162166 #162166
User since
2012-09-18
5 Artikel
BenutzerIn
[default_avatar]
Vielen Dank noch mal!

Mit eurer Hilfe habe ich die naechste Anforderung geloest:

Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#!/usr/bin/perl

use strict;
use warnings;
use Data::Dumper;

my @array = (
    { 'ccc' => [ {'c' => 'c1'}, {'c' => 'c2'}, {'xx' => 'c3'} ] }, 
    { 'aaa' => [ {'a' => 'a1'}, {'a' => 'a2'}, {'xx' => 'a3'} ] }, 
    { 'bbb' => [ {'b' => 'b1'}, {'b' => 'b2'}, {'xx' => 'b3'} ] }, 
);

my @temp = map { $_->[0] }
          sort { $a->[1] cmp $b->[1] }
           map { [ $_, find_val($_) ] } @array;

sub find_val {
    for my $ref ($_) {
        for my $element (keys %$ref) {
            return $ref->{$element}->[2]->{'xx'};
        }  
    }  
}

print Dumper(@temp);


Gruss
--@devrand

View full thread Sort Array of Hashes by Key