If all you're interested in is a short way (without having to type in the lines of a loop structure), then the following is a way to do it (there may be shorter ways):

#!/usr/bin/perl use strict; use warnings; my $y= 10; my @array =(1, 4, 5, 7, 8, 9, 10, 20, 30, 40, 60); my $greater = 50 ; my $num = scalar(@array); my $percent = (scalar(grep{$_ > $greater} @array)/$num)*100; print "Percent of \@array's $num items greater than $greater is $perce +nt%\n"; exit(0);

Of course you can shorten it even more by putting the scalar(@array) directly in the division in plac of the $num that I use. I did it that way so that I could report the number of elemnts in the array in the print statement.

But overall I completely agree with ikegami in as much as several folks of late have asked how to things 'without looping' but that reqire 'looping' to accomplish. The answer, in general, is that 'you can't'. So I have been interpreting the questions to mean either (1) 'how do I do it without having to type in the loop structures' and/or (2) 'Is there a way that perhaps the compiler (interpreter) can do it for me more efficiently'.

I don't knowwhat the OP was looking for, but hopefully we answered the question. Good luck,

ack Albuquerque, NM

In reply to Re: Calculated % of data greater than $x in an array by ack
in thread Calculated % of data greater than $x in an array by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.