in reply to Finding a variable string within a variable string.

It appears that my question wasn't as simple as I thought it might be. That maybe because I didn't express myself clearly so I'll have another go

I haven't provided samples because

I know that m/string/ will tell me if "string" is in $_, and I assume m/$string/ will tell me if the string stored in $string is in $_, but what I need to know is the syntax for finding out if $string1 contains $string2 anywhere (something like C's strstr function). I don't need to know where in $string1 $string2 was found, nor do I want $string1 changed unless I find $string2 within it, I only need to know if it was found.

If I still haven't explained myself properly, or more information is needed, I will try to come up with examples that don't contain propriety information.

Moriarty

  • Comment on Re: Finding a variable string within a variable string.

Replies are listed 'Best First'.
Re^2: Finding a variable string within a variable string.
by Eimi Metamorphoumai (Deacon) on Dec 14, 2004 at 21:13 UTC
    You can either use regexps, or index
    if ($string1 =~ /\Q$string2\E/){...} if (index($string1, $string2) != -1){...}
    The \Q and \E are to quote metacharacters (so if $string2 is "f.o", you don't end up matching on "foo").

      Thank you, I had an idea that it would be that simple but I was being a bit thick due to the lateness in the day. After working on these problems all day, the brain gets a little numb.

      Moriarty