How to match string inside of array?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • somebody

    How to match string inside of array?

    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


  • Jürgen Exner

    #2
    Re: How to match string inside of array?

    somebody wrote:[color=blue]
    > 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.[/color]

    perldoc -q contains

    jue


    Comment

    • somebody

      #3
      Re: How to match string inside of array?

      On Fri, 16 Dec 2005 23:19:28 +0000, Jürgen Exner wrote:
      [color=blue]
      > somebody wrote:[color=green]
      >> 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.[/color]
      >
      > perldoc -q contains
      >
      > jue[/color]


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

      mybox:> perldoc -q contains
      No documentation for perl FAQ keyword `contains' found


      Comment

      • Jim Gibson

        #4
        Re: How to match string inside of array?

        In article <4YHof.11993$fY 5.2618@trnddc02 >, Jürgen Exner
        <jurgenex@hotma il.com> wrote:
        [color=blue]
        > somebody wrote:[color=green]
        > > 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.[/color]
        >
        > perldoc -q contains[/color]

        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 **
        ----------------------------------------------------------
        Best Usenet Service Providers 2025 ranked by Newsgroup Access Newsservers, Usenet Search, Features & Free Trial. Add VPN for privacy.

        Comment

        • Jürgen Exner

          #5
          Re: How to match string inside of array?

          somebody wrote:[color=blue]
          > On Fri, 16 Dec 2005 23:19:28 +0000, Jürgen Exner wrote:
          >[color=green]
          >> somebody wrote:[color=darkred]
          >>> How do I check if $myvar matches 'two' in @arr?[/color]
          >>
          >> perldoc -q contains[/color]
          >
          > I don't seem to have that. Is it a separate module?
          >
          > mybox:> perldoc -q contains
          > No documentation for perl FAQ keyword `contains' found[/color]

          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


          Comment

          Working...