P is for Practical PerlMonks

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

by nvivek (Vicar)
 on Apr 08, 2010 at 05:16 UTC ( #833441=note: print w/replies, xml ) Need Help??

You try this,
```use strict;
use warnings;

my @array=(34,52,67,3,66);

my \$temp;
map { \$temp=\$_ if \$_ > 10 } @array;
print "Yes,at least one number is bigger than 10\n" if(\$temp);
The above map operation will check any element of the array and store it to temp if it is greater than 10. Finally,we will check whether the temp is containing value or not.If it isn't undef,printing the message as greater than 10 in an array. If you don't want to store any temporary variable,then you try this.
```use strict;
use warnings;

my @array=(34,52,67,3,66);
map { print "Yes,at least one number is bigger than 10\n" and exit if
+\$_ > 10 } @array;

Replies are listed 'Best First'.
Re^2: Check if at least one element of array is bigger than X
by ikegami (Pope) on Apr 08, 2010 at 06:22 UTC
I must agree with GrandFather. This is the grep solution gone horribly wrong.
Re^2: Check if at least one element of array is bigger than X
by GrandFather (Sage) on Apr 08, 2010 at 05:43 UTC

How is either solution better than any of the other solutions that have been provided?

True laziness is hard work

Create A New User
Node Status?
node history
Node Type: note [id://833441]
help
Chatterbox?
 [Cosmic37]: sort of anyway... hurls some peaches... :-D [Cosmic37]: when slurping one can undef the record separator thingy; but presumably one should redefine it afterwards? how? [Cosmic37]: I'm wondering whether there is a specific/(or at least "usual") command or does one take a copy before undefining and then copy it back after slurping file into a string? [BarApp]: I need help accessing perl files. I need permission!!!

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (4)
As of 2017-06-29 16:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How many monitors do you use while coding?

Results (672 votes). Check out past polls.