On Oct 30, [EMAIL PROTECTED] said:

>$test = "one day I went to the zoo";
>
>and I want to find how many characters into $test the word "went" is, how 
>do I go about that?

The simple index() function:

  if (($pos = index($test, "went")) > -1) {
    print "'went' was found at position $pos\n";
  }
  else {
    print "'went' was not found\n";
  }

The only problem is that you will also find "twenty".  If you want greater
control, you'll need a regex:

  # requires Perl 5.6
  if ($test =~ /\bwent\b/) {
    $pos = $-[0];
  }

  # or, for pre-5.6 Perls
  if ($test =~ /(.*?)\bwent\b/) {
    $pos = length $1;
  }

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to