On Wednesday 04 March 2009 15:12:48 Berler Chanan wrote:
> Hi All,
>
> I wonder why/or should I ask "if" there is an operator IN in perl.
> For example:
>
> my @arr = (1,2,3,4,5);
> if (1 in @arr)
> {
> # do something
> }
>
See any from List::MoreUtils :
http://search.cpan.org/dist/List-MoreUtils/
{{{{{{{{{{{{
#!/usr/bin/perl
use strict;
use warnings;
use List::MoreUtils qw(any);
if (any { $_ == 5 } (1,7,5,100,200))
{
print "List 1: 5 is there\n";
}
else
{
print "List 1: No 5\n";
}
if (any { $_ == 5 } (1,7,500,100,200))
{
print "List 2: 5 is not there\n";
}
else
{
print "List 2: No 5\n";
}
}}}}}}}}}}}}}}}}
Tested.
If you use it often, you should create a function that does what you want
according to what you want (i.e: "==", "eq", "=~", etc.).
A few other notes:
1. If you're going to do it often, you should use a hash instead.
2. perl-5.10.x gives you the ~~ operator. I personally don't like it since
it's hard to tell whether it does a string or a numeric comparison.
Regards,
Shlomi Fish
> The idea came to me when using SQL statement: select …. where element in
> (1,2,3,4,5) PS: the use of foreach and check is tooooooooo long for me ☺
>
> Thanks
> Chanan
>
> _______________________________________________
> Perl mailing list
> [email protected]
> http://perl.org.il/mailman/listinfo/perl
--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
My Aphorisms - http://www.shlomifish.org/humour.html
<mauke> I'm not interested in what you're doing; what are you trying to
achieve?
<PerlJam> mauke: I'm trying to achieve world peace and this regex is
the last thing standing in my way! ;)
_______________________________________________
Perl mailing list
[email protected]
http://perl.org.il/mailman/listinfo/perl