I like Grandfather's solution. It uses the scalar value of grep in an "if" statement and the simplicity of this is normally the best way to go. If @array is very big, here is one way to stop processing once you have a "match". This speeds things up at the cost of complexity.
#!usr/bin/perl -w use strict; my @array=(34,52,67,3,66); is_value_gt_min_array_element(5,\@array) ? print "yes, 5 is greater than @array\n" : print "no, 5 isn't greater than: @array\n"; is_value_gt_min_array_element(3,\@array) ? print "yes, 3 is greater than @array\n" : print "no, 3 isn't greater than: @array\n"; is_value_gt_min_array_element(2,\@array) ? print "yes, 2 is greater than @array\n" : print "no, 2 isn't greater than: @array\n"; sub is_value_gt_min_array_element { my ($value, $arrayref) = @_; foreach (@$arrayref) { return 1 if ($value > $_); #this is a "found it!" #stop searching for another one } return 0; } print "There are ", scalar(grep{$_>3}@array), " numbers ", "greater than 3 in: @array\n"; __END__ prints: yes, 5 is greater than 34 52 67 3 66 no, 3 isn't greater than: 34 52 67 3 66 no, 2 isn't greater than: 34 52 67 3 66 There are 4 numbers greater than 3 in: 34 52 67 3 66

In reply to Re: Check if at least one element of array is bigger than X by Marshall
in thread Check if at least one element of array is bigger than X 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.