[Israel.pm] checking for presence of a value in a list

Peter Gordon peter at pg-consultants.com
Wed Aug 15 22:22:43 PDT 2007




On Wed, 2007-08-15 at 18:42 +0300, Ran Eilam wrote:
> On 8/15/07, Amir E. Aharoni <amir.aharoni at gmail.com> wrote:
> > grep may be clever, but it iterates over the whole list, and i don't need it.
>                                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> 
> On 8/15/07, Peter Gordon <peter at pg-consultants.com> wrote:
> > @w = (1, 2, 3, 4, 5) ;
> > grep { $w->{$_} ++ } @w ;
> > print "found\n" if exists $w->{3} ;
> 
> On 8/15/07, Eli Billauer <eli at billauer.co.il> wrote:
> > @w = (1, 2, 3, 4, 5) ;
> > print "Found\n" if (grep { $_ == 3 } @w);
> 
> He said he does not want to iterate over the entire list- read the
> question! The list could be big, and the item you want could be the
> 1st.
> 
> Any hash-based solution suffers from the exact same problem.
> 
This is true. OTOH, if he needs do to it several times, there is going
to be a cutoff where it may be more efficient to set up the hash.


> 
> On 8/15/07, Shlomi Fish <shlomif at iglu.org.il> wrote:
> > use List::MoreUtils qw(any);
> > my @set = (1, 2, 3, 4, 5);
> > if (any { $_ == 3 } @set) {  print "found\n" }
> 
> Is best, and it's core.
> 
> If you need junctions (which is the CompSci name, not "quantum"),
> there's Perl6::Junction, for Perl5, which is simpler than
> Quantum::Superpositions. Of course a script with the word
> "superposition" in it, is way cooler.
> 
> It's actually useful for many things bigger than finding an element in a list.
> 
> Ran
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://perl.org.il/mailman/listinfo/perl




More information about the Perl mailing list