Searching an @array for index of Regex Match

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • poojapo
    New Member
    • Feb 2007
    • 6

    Searching an @array for index of Regex Match

    hi guys,

    how do i search for a pattern(which is a regular expression) inside an array element and get the position of the occurence of it?

    @array is the array.I am at element $array[$n].$n here is a subscript.
    how do i search for pattern v*?(a reg ex)
  • docsnyder
    New Member
    • Dec 2006
    • 88

    #2
    @poojapo

    Try this approach:
    Code:
    @array = ( "1", "2", "3", "2", "4" );
    
    for ( $i=0 ; $i<scalar(@array) ; $i++ ) {
      printf("$i\n") if ( $array[$i] =~ m/2/ );
    }
    Greetz, Doc

    By the way: /v*?/ is not a lucky pattern as it always matches!

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      if you literally mean look for the pattern v*? in @array:

      Code:
      for (@array) {
         print "$_\n" if  /\Qv*?\E/;
      }
      or:

      Code:
      if (grep {/\Qv*?\E/ } @array) {
         print "Found 'v*?' in \@array\n";
      }

      Comment

      Working...