<?xml version="1.0" encoding="windows-1252"?>
<node id="837448" title="Re: Top and bottom 10 percent elements of an array" created="2010-04-29 01:23:56" updated="2010-04-29 01:23:56">
<type id="11">
note</type>
<author id="711414">
nagalenoj</author>
<data>
<field name="doctext">
&lt;p&gt;
This works better for the given elements. It gives the result as you need.
Refer &lt;a href="http://perldoc.perl.org/functions/splice.html"&gt;splice&lt;/a&gt; to know about it.
&lt;/p&gt;

&lt;code&gt;
my @array = (2 ,4, 3, 8, 9, 12, 13, 20, 18, 7 );

my $percent = (scalar @array * 0.20);
my (@resultA, @resultB);

my @ordered = sort {$a &lt;=&gt; $b} @array;

@resultB = splice(@ordered, 0, $percent);
@resultA = splice(@ordered, (scalar @ordered - $percent));
print "@array", "\n";

for (my $i=0; $i &lt; scalar @array; $i++)  {
    if ( grep { $array[$i] eq $_ } @resultA ) {
        $array[$i] = 'A';
    }
    elsif ( grep { $array[$i] eq $_ } @resultB ) {
        $array[$i] = 'B';
    }
    else  {
        $array[$i] = '-';
    }
}

print "@array", "\n";

&lt;/code&gt;</field>
<field name="root_node">
837442</field>
<field name="parent_node">
837442</field>
</data>
</node>
