Tolgar
asked on
How to check eleements of an array in Perl?
Hi,
Let's say I have an array called resultList.
I have some elements in this array. (Let's say 1,1,1,1,1,1,0,1,1,1,1,.... ..)
I would like to check if any elements of this array (we don't know the length of the array)
has a a value of 0.
How can I do it in Perl? Are there any Perl functions that does this?
Thanks,
Let's say I have an array called resultList.
I have some elements in this array. (Let's say 1,1,1,1,1,1,0,1,1,1,1,....
I would like to check if any elements of this array (we don't know the length of the array)
has a a value of 0.
How can I do it in Perl? Are there any Perl functions that does this?
Thanks,
ASKER
Can I do something like this?
if ('0' eq any(@resultList))
That is Python like :)
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
So, there is no built in function in Perl to do this. Am I right?
Thanks,
Thanks,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
if( grep{$_==0} @array ){
}
}
#or
use Quantum::Superpositions;
if( 0==any @array ){
}
use Quantum::Superpositions;
if( 0==any @array ){
}
Personally, I'm a fan of first (in List::Utils I think). It's more efficient than grep (and I'm guessing any).
use List::Utils qw(first);
if (first {$_==0} @array) {
}
Yes, it more efficient, but grep is a core built-in function, as is the more efficient for this purpose ~~ operator in v5.10.1 or later.
Quantum::Superpositions is neither built-in, nor efficient (unless you have perl running on a quantum computer) but it has the syntax of http:#a38172275
Quantum::Superpositions is neither built-in, nor efficient (unless you have perl running on a quantum computer) but it has the syntax of http:#a38172275
Open in new window
Otherwise you will have to test one element at a time.