Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Top and bottom 10 percent elements of an array

by nagalenoj (Friar)
on Apr 29, 2010 at 05:23 UTC ( #837448=note: print w/replies, xml ) Need Help??


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

This works better for the given elements. It gives the result as you need. Refer splice to know about it.

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 <=> $b} @array; @resultB = splice(@ordered, 0, $percent); @resultA = splice(@ordered, (scalar @ordered - $percent)); print "@array", "\n"; for (my $i=0; $i < 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";

Replies are listed 'Best First'.
Re^2: Top and bottom 10 percent elements of an array
by sesemin (Beadle) on Apr 29, 2010 at 05:31 UTC
    Thanks, easy to implement and easy to understand.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://837448]
help
Chatterbox?
[beech]: checking chatterbox sidebar
[beech]: and again a check on chatterbox sidebar, what is expandability

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2017-03-27 22:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should Pluto Get Its Planethood Back?



    Results (324 votes). Check out past polls.