Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Top and bottom 10 percent elements of an array

by ikegami (Patriarch)
on Apr 29, 2010 at 05:53 UTC ( [id://837453]=note: print w/replies, xml ) Need Help??


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

It's simpler if you sort the indexes instead of the values.

my $portion = 0.20; my @array = (2, 4, 3, 8, 9, 12, 13, 20, 18, 7); my $keep = int(@array * $portion); my @sorted_idxs = sort { $array[$a] <=> $array[$b] } 0..$#array; my @final = ('-') x @array; $final[$sorted_idxs[$_]] = 'B' for 0..$keep-1; $final[$sorted_idxs[$_]] = 'A' for -$keep..-1; print("@final\n");
B - B - - - - A A -

Update: Fixed off-by-one error.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://837453]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2024-04-19 07:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found