print matching elements of a list

Discussion in 'Perl Misc' started by starwars, Feb 1, 2004.

  1. starwars

    starwars Guest

    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.
     
    starwars, Feb 1, 2004
    #1
    1. Advertising

  2. In article <>,
    starwars <> 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.
     
    Walter Roberson, Feb 1, 2004
    #2
    1. Advertising

  3. starwars

    Bill Guest

    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 } }
     
    Bill, Feb 1, 2004
    #3
  4. starwars

    kj Guest

    In <bvhijc$41o$> -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
     
    kj, Feb 1, 2004
    #4
  5. starwars

    Ben Morrow Guest

    kj <> wrote:
    > In <bvhijc$41o$>
    > -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)
    * *
     
    Ben Morrow, Feb 1, 2004
    #5
  6. On Sun, 1 Feb 2004 13:14:01 +0000 (UTC), kj <> 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"
     
    Michele Dondi, Feb 2, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. omission9
    Replies:
    4
    Views:
    328
    Rene Pijlman
    Jan 24, 2004
  2. Adam Hartshorne
    Replies:
    2
    Views:
    386
    Nitin Motgi
    Jan 27, 2006
  3. keto
    Replies:
    0
    Views:
    1,018
  4. David Cournapeau

    print a vs print '%s' % a vs print '%f' a

    David Cournapeau, Dec 30, 2008, in forum: Python
    Replies:
    0
    Views:
    386
    David Cournapeau
    Dec 30, 2008
  5. Xavier Noria

    print LIST vs print join "", LIST

    Xavier Noria, Apr 16, 2004, in forum: Perl Misc
    Replies:
    14
    Views:
    204
    Uri Guttman
    Apr 19, 2004
Loading...

Share This Page