How to match string inside of array?

Discussion in 'Perl' started by somebody, Dec 16, 2005.

  1. somebody

    somebody Guest

    If I have an array like:

    @arr = ('one', 'two', 'three');

    And the varaible $myvar = 'two';


    How do I check if $myvar matches 'two' in @arr?

    I know I could iterate through the array with
    a foreach statement, but was wondering if there
    was a more efficient method.

    -Thanks
    somebody, Dec 16, 2005
    #1
    1. Advertising

  2. somebody wrote:
    > How do I check if $myvar matches 'two' in @arr?
    >
    > I know I could iterate through the array with
    > a foreach statement, but was wondering if there
    > was a more efficient method.


    perldoc -q contains

    jue
    Jürgen Exner, Dec 16, 2005
    #2
    1. Advertising

  3. somebody

    somebody Guest

    On Fri, 16 Dec 2005 23:19:28 +0000, Jürgen Exner wrote:

    > somebody wrote:
    >> How do I check if $myvar matches 'two' in @arr?
    >>
    >> I know I could iterate through the array with
    >> a foreach statement, but was wondering if there
    >> was a more efficient method.

    >
    > perldoc -q contains
    >
    > jue



    I don't seem to have that. Is it a separate module?

    mybox:> perldoc -q contains
    No documentation for perl FAQ keyword `contains' found
    somebody, Dec 16, 2005
    #3
  4. somebody

    Jim Gibson Guest

    In article <4YHof.11993$fY5.2618@trnddc02>, Jürgen Exner
    <> wrote:

    > somebody wrote:
    > > How do I check if $myvar matches 'two' in @arr?
    > >
    > > I know I could iterate through the array with
    > > a foreach statement, but was wondering if there
    > > was a more efficient method.

    >
    > perldoc -q contains


    In my version (5.8.6) you have to search for "contained"

    FYI to OP: this newsgroup is defunct; try comp.lang.perl.misc in the
    future.

    Posted Via Usenet.com Premium Usenet Newsgroup Services
    ----------------------------------------------------------
    ** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
    ----------------------------------------------------------
    http://www.usenet.com
    Jim Gibson, Dec 16, 2005
    #4
  5. somebody wrote:
    > On Fri, 16 Dec 2005 23:19:28 +0000, Jürgen Exner wrote:
    >
    >> somebody wrote:
    >>> How do I check if $myvar matches 'two' in @arr?

    >>
    >> perldoc -q contains

    >
    > I don't seem to have that. Is it a separate module?
    >
    > mybox:> perldoc -q contains
    > No documentation for perl FAQ keyword `contains' found


    Strange. I get

    E:\>perldoc -q contains
    Found in E:\[...]\perlfaq4.pod
    How can I tell whether a list or array contains a certain element?


    jue
    Jürgen Exner, Dec 17, 2005
    #5
    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. hiwa
    Replies:
    0
    Views:
    624
  2. Victor
    Replies:
    2
    Views:
    625
    Victor
    May 17, 2004
  3. ekzept
    Replies:
    0
    Views:
    351
    ekzept
    Aug 10, 2007
  4. John Gordon
    Replies:
    13
    Views:
    452
    Ian Kelly
    Dec 20, 2011
  5. Old Echo
    Replies:
    1
    Views:
    170
    Adam Shelly
    Sep 4, 2008
Loading...

Share This Page