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

Ran Eilam ran.eilam at gmail.com
Wed Aug 15 08:42:42 PDT 2007


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.


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



More information about the Perl mailing list