my @LIST = map { [ $_->[0], $_->[1], $_->[2], $_->[3], $_->[4], $_->[5],$_->[6] ] } sort { $a->[0] cmp $b->[0] || $b->[4] <=> $a->[4] || $a->[1] cmp $b->[1] || $a->[2] cmp $b->[2] || $a->[3] cmp $b->[3] } map { my $group = $_; map { my $host = $_; map { my $service = $_; map { my $state = $_; my $count = keys %{$ERROR1{$group}->{$host}->{$service}->{$state}}; my $ts1 = (sort (keys %{$ERROR1{$group}->{$host}->{$service}->{$state}}))[0]; my $ts2 = (sort(keys %{$ERROR1{$group}->{$host}->{$service}->{$state}}))[$count]; [ $group, $host, $service, $state, $count, $ts1, $ts2 ] } keys %{$ERROR1{$group}->{$host}->{$_}} } keys %{$ERROR1{$group}->{$_}} } keys %{$ERROR1{$_}} } keys %ERROR1;