[Israel.pm] Is there an operator like IN in perl

Richie Sevrinsky richie at mamash.com
Wed Mar 4 06:44:29 PST 2009


In terms of teaching a man to fish (no pun intended), pointing in the
direction of standard function grep is far more educationally worthwhile
than the non-standard module List::MoreUtils (or even than standard
module List::Utils). Yes, the specialized functions of
List::Utils::first or List::MoreUtils::any may be more optimized for the
specific requested behavior (as would a hash, as mentioned further in
the thread), but I would contend that elementary knowledge of grep is
more essential to Perl expertise.


Incidentally, looking at the implementations, I can't figure out why
anyone would use List::MoreUtils::any  over List::Utils::first. They do
exactly the same thing, except that first returns the matching value.


- Richie


Shlomi Fish wrote:

> 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
>>> Perl at perl.org.il
>>> http://perl.org.il/mailman/listinfo/perl
>>>       
>> _______________________________________________
>> Perl mailing list
>> Perl at perl.org.il
>> http://perl.org.il/mailman/listinfo/perl
>>     
>
>   



More information about the Perl mailing list