print matching elements of a list

S

starwars

I want to print x, if a $var matches any of the elements of a list.

How do I write that?

I am not any kind of a programmer, but am trying to just get some very basic
stuff done on my linux box.

Thx.
 
W

Walter Roberson

:I want to print x, if a $var matches any of the elements of a list.

:How do I write that?

Is that matches as in a regular expression, or is that matches
as in equality?

If it is regular expressions, then

perldoc -q "How do I efficiently match many regular expressions at once?"
to get the answer from the perl faq.

If it is equality, then

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

for a discussion of several techniques from the perl faq.
 
B

Bill

starwars said:
I want to print x, if a $var matches any of the elements of a list.

How do I write that?

I am not any kind of a programmer, but am trying to just get some very basic
stuff done on my linux box.

Thx.

Uses builtin grep function, assumes you want to find ALL matches:

my @matched = grep { $var eq $_ } @list;
print 'x' if scalar @matched;

More efficient if the match is early in @list:

foreach (@list) { if ($var eq $_} { print 'x'; last } }
 
K

kj

In said:
If it is equality, then
perldoc -q "How can I tell whether a certain element is contained in a list or array?
for a discussion of several techniques from the perl faq.


I'm interested in this FAQ, but when I tried the query above I got

No documentation for perl FAQ keyword `How can I tell whether a certain element is contained in a list or array?' found

I tried truncating the string in obvious ways, in the hope that
one of the substrings would lead to the FAQ, but no dice. Any
other keywords I can use instead?

kj
 
B

Ben Morrow

kj said:
In <[email protected]>




I'm interested in this FAQ, but when I tried the query above I got

No documentation for perl FAQ keyword `How can I tell whether a
certain element is contained in a list or array?' found

I tried truncating the string in obvious ways, in the hope that
one of the substrings would lead to the FAQ, but no dice. Any
other keywords I can use instead?

It should be in perlfaq4, and should come up with perldoc -q
contained. If it doesn't, take a look at perlfaq4 on
http://www.perldoc.com/.

Ben
 
M

Michele Dondi

I tried truncating the string in obvious ways, in the hope that
one of the substrings would lead to the FAQ, but no dice. Any
other keywords I can use instead?

perldoc -q contained


Michele
 

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

Forum statistics

Threads
474,266
Messages
2,571,085
Members
48,773
Latest member
Kaybee

Latest Threads

Top