http://www.perlmonks.org?node_id=837456


in reply to Top and bottom 10 percent elements of an array

How about a solution with map.
@array = (2 ,4, 3, 8, 9, 12, 13, 20, 18, 7 ); my $c=0; @sort_a_pos = sort {$a->[1]<=>$b->[1]} map { [$c++ => $_] } @array; $pct = @array / 10; $_->[1] = "B" for @sort_a_pos[0 .. $pct]; $_->[1] = "-" for @sort_a_pos[$pct+1 .. $#array-$pct-1]; $_->[1] = "A" for @sort_a_pos[$#array-$pct .. $#array]; @finalarray = map {$_->[1] } sort { $a->[0] <=> $b->[0] } @sort_a_pos +; print "@finalarray";

print+qq(\L@{[ref\&@]}@{['@'x7^'!#2/"!4']});

Replies are listed 'Best First'.
Re^2: Top and bottom 10 percent elements of an array
by ikegami (Patriarch) on Apr 29, 2010 at 18:38 UTC
    $_->[1] = "B" for @sort_a_pos[0 .. $pct]; $_->[1] = "-" for @sort_a_pos[$pct+1 .. $#array-$pct-1]; $_->[1] = "A" for @sort_a_pos[$#array-$pct .. $#array];
    should be
    $_->[1] = "B" for @sort_a_pos[0 .. $pct-1]; $_->[1] = "-" for @sort_a_pos[$pct .. $#array-$pct-1]; $_->[1] = "A" for @sort_a_pos[$#array-$pct .. $#array];

    You probably got confused (like me) by the OP's weird math of 10 * 10% = 2. The output he gave was for 20%.