Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > print matching elements of a list

Reply
Thread Tools

print matching elements of a list

 
 
starwars
Guest
Posts: n/a
 
      02-01-2004
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.

 
Reply With Quote
 
 
 
 
Walter Roberson
Guest
Posts: n/a
 
      02-01-2004
In article <(E-Mail Removed) ux.net>,
starwars <(E-Mail Removed)> wrote:
: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.
--
Contents: 100% recycled post-consumer statements.
 
Reply With Quote
 
 
 
 
Bill
Guest
Posts: n/a
 
      02-01-2004
starwars wrote:
> 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 } }



 
Reply With Quote
 
kj
Guest
Posts: n/a
 
      02-01-2004
In <bvhijc$41o$(E-Mail Removed)> http://www.velocityreviews.com/forums/(E-Mail Removed)-cnrc.gc.ca (Walter Roberson) writes:
>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
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      02-01-2004

kj <(E-Mail Removed)> wrote:
> In <bvhijc$41o$(E-Mail Removed)>
> (E-Mail Removed)-cnrc.gc.ca (Walter Roberson) writes:
> >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?


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

--
And if you wanna make sense / Whatcha looking at me for? (Fiona Apple)
* http://www.velocityreviews.com/forums/(E-Mail Removed) *
 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      02-02-2004
On Sun, 1 Feb 2004 13:14:01 +0000 (UTC), kj <(E-Mail Removed)> wrote:

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

>
>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
--
you'll see that it shouldn't be so. AND, the writting as usuall is
fantastic incompetent. To illustrate, i quote:
- Xah Lee trolling on clpmisc,
"perl bug File::Basename and Perl's nature"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Appending a list's elements to another list using a list comprehension Debajit Adhikary Python 17 10-18-2007 06:45 PM
Removing elements from a list that are elements in another list Adam Hartshorne C++ 2 01-27-2006 07:47 AM
Unlarging the print to print using PDF file to print Bun Mui Computer Support 3 09-13-2004 03:15 AM
print LIST vs print join "", LIST Xavier Noria Perl Misc 14 04-19-2004 01:44 PM
efficient matching of elements a list omission9 Python 4 01-24-2004 11:19 AM



Advertisments