Operator for presence of value in list?

M

Martin Mrazek

Hi,
I'm new to Perl. Is there something like in operator?

$a in @list; returns 1 if $a is among values in @list, 0 if not.

I'm just lazy to write foreach $elem (@list) { if ($a eq $elem) {
return 1;} } return 0;

Martin
 
G

Gunnar Hjalmarsson

Martin said:
I'm new to Perl.

Then you should start learning how to make use of the documentation.
Is there something like in operator?

$a in @list; returns 1 if $a is among values in @list, 0 if not.

You just asked a Frequently Asked Question.

perldoc -q contained

"How can I tell whether a certain element is contained in a
list or array?"
 
G

Gunnar Hjalmarsson

Bernard said:
perldoc -f grep

Quote from the applicable Perl FAQ ("perldoc -q contained"):

"Please do not use

($is_there) = grep $_ eq $whatever, @array;

or worse yet

($is_there) = grep /$whatever/, @array;

These are slow (checks every element even if the first matches),
inefficient (same reason), and potentially buggy (what if there are
regex characters in $whatever?)."
 
T

Tad McClellan

[ Please do not post TOFU style, it annoys folks here.
Text trimmed and rearranged into a sensible order.

}
I'm even more lazy than Martin, why does nobody copy-paste the faq here?


Because posters that require that someone _else_ read the FAQs to
them deserves to remain in ignorance.

There is Good Lazy and there is Bad Lazy...
 
C

ctcgag

Gunnar Hjalmarsson said:
Quote from the applicable Perl FAQ ("perldoc -q contained"):

"Please do not use

($is_there) = grep $_ eq $whatever, @array;

or worse yet

($is_there) = grep /$whatever/, @array;

These are slow (checks every element even if the first matches),
inefficient (same reason),

Why not just say "Don't use Perl, it is slow and inefficient."?

If one doesn't care enough about efficiency to use a hash, then I highly
doubt one will care about the (generally trivial) additional inefficiency
of using grep. I've probably spent more of my time typing this reply than
all of the time wasted via grep-in-boolean-context of all the perl programs
I've ever run.
and potentially buggy (what if there are
regex characters in $whatever?)."

Both forms (the grep and the foreach loop) require you to make the correct
decision regarding use of eq versus =~ versus ==. If you habitually use
regex when you shouldn't be, then the suggested replacement is just as
"potentially buggy" as what it is replacing.

Xho
 
B

Brian McCauley

Why not just say "Don't use Perl, it is slow and inefficient."?

If one doesn't care enough about efficiency to use a hash, then I highly
doubt one will care about the (generally trivial) additional inefficiency
of using grep. I've probably spent more of my time typing this reply than
all of the time wasted via grep-in-boolean-context of all the perl programs
I've ever run.

I agree completely with what Xho just said.

Indeed I find now that in c.l.p.* now I agree with Xho more of the time
than I agree with any other single member of the regular crowd (Anno,
Tad, Uri, Gunnar...).

Hmmm... I think I'll just wander off to see how much I agree with Xho on
other issues...

http://groups.google.com/groups?q=author:[email protected]

--
\\ ( )
. _\\__[oo
.__/ \\ /\@
. l___\\
# ll l\\
###LL LL\\
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,880
Members
47,417
Latest member
DarrenGaun

Latest Threads

Top