in reply to Calculated % of data greater than $x in an array

Assuming your input array is sorted numerically (as in your code), you can use the firstidx function from List::MoreUtils to get the index of the first element greater than a specified value. This function will stop looking through the array as soon as the condition is met.

If your input array is not ordered, you must inspect every element. grep is very handy for that task.

use strict; use warnings; use List::MoreUtils qw (firstidx); my $y = 10; my @array = (1, 4, 5, 7, 8, 9, 10, 20, 30, 40, 60); my $tot = scalar @array; my $i = firstidx { $_ > $y } @array; printf "%d%%", 100*($tot-$i)/$tot; __END__ 36%