in reply to Find a string within a string

Apologies all, error in the code I submitted in initial message The code should read
$line='123: ABC/1.0/VAL 111:222:333:444'; $cont='ABC/1.0/VAL 111:222:333:444'; if ($line=~/$cont/) { print "We have a match\n"; } else { print "No match\n"; }
Thanx, Val

Replies are listed 'Best First'.
Re: Re: Correction to 1st -Find a string within a string
by rasta (Hermit) on Nov 13, 2002 at 12:11 UTC
    That code returns "We have a match". Although I'm think you hould use \Q and \E in comparison to avoid troubles with other strings.
    So the code should look like this:
    $line='123: ABC/1.0/VAL 111:222:333:444'; $cont='ABC/1.0/VAL 111:222:333:444'; if ($line=~/\Q$cont\E/) { print "We have a match\n"; } else { print "No match\n"; }
Re: Corection to 1st message entitled -Find a string within a string
by rdfield (Priest) on Nov 13, 2002 at 13:00 UTC
    Try it using quotemata:
    $cont='\QABC/1.0/VAL 111:222:333:444\E';
    since you're looking for the literal string.

    rdfield