On Wednesday 04 March 2009 15:18:34 Richie Sevrinsky wrote:
> Use grep as follows:
>
> my @arr = (1,2,3,4,5);
> if (grep($_ == 1, @arr))
>
> I recommend reading "perldoc -f grep".
>
The problem with grep as demonstrated like this, is that it will iterate over
all the list and collect *all* occurrence of matching elements. In your case,
after it found the "1", it will go over 2, 3, 4 and 5, and find nothing. any
from List::MoreUtils will stop after the first occurrence.
grep is still useful for *filtering* a list, but it should not be used for
checking for the existence of a single element.
Regards,
Shlomi Fish
> - Richie
>
> 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
> > }
> >
> > 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
>
> _______________________________________________
> Perl mailing list
> [email protected]
> http://perl.org.il/mailman/listinfo/perl
--
-----------------------------------------------------------------
Shlomi Fish http://www.shlomifish.org/
Best Introductory Programming Language - http://xrl.us/bjn84
<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