Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'm searching for a "substring" within another string. Something like this....
$source = "/kmtest/cci022_031902.txt: No such file or directory"; if ($source =~ /'no such file'/i) { print "Found it \n"; }
I want a "true" or "false" status, however, this dosen't seem to be working. Any help would be appreciated. I'm a newcomer to the Perl arena, Thanks. KM

Replies are listed 'Best First'.
(jeffa) Re: string search
by jeffa (Bishop) on Mar 20, 2002 at 14:55 UTC
    You need to take out your single quotes:
    if ($source =~ /no such file/i) {

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: string search
by broquaint (Abbot) on Mar 20, 2002 at 14:58 UTC
    If you're just doing plain substring matching then index() might be a simpler option
    if(index($source, 'no such file') != -1) { print "Found it \n"; }

    HTH

    broquaint

      index is case sensitive. You'll probably want to say
      if(index(lc $source, 'no such file') != -1) { print "Found it \n"; }
      to normalise $source into lower case.
Re: string search
by busunsl (Vicar) on Mar 20, 2002 at 14:56 UTC
    Drop the quotes (') inside of the regex.

    Like this:

    $source = "/kmtest/cci022_031902.txt: No such file or directory"; if ($source =~ /no such file/i) { print "Found it \n"; }