Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Check if at least one element of array is bigger than X

by syphilis (Canon)
on Apr 08, 2010 at 06:44 UTC ( #833450=note: print w/ replies, xml ) Need Help??


in reply to Check if at least one element of array is bigger than X

but not print it 4 times

The other solutions are better, but you could just print the message the first time only (untested):

my $mess = 1; foreach $i(@array) { if($i > 10) { print "Bigger than 10" if $mess; $mess = 0; } } $mess = 1; # In case we want to use it again.
This would allow you to (if needed) iterate through the whole loop, but display the message only once.

Cheers,
Rob


Comment on Re: Check if at least one element of array is bigger than X
Download Code
Re^2: Check if at least one element of array is bigger than X
by eric256 (Parson) on Apr 09, 2010 at 14:56 UTC

    More than one way to skin a cat:

    #!/usr/bin/perl use strict; use warnings; my @array = (1..10); my $found = 0; for (@array) { $found ++ if $_ > 10; } print "Bigger than 10" if $found;

    ___________
    Eric Hodges

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2015-07-05 23:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (68 votes), past polls