in reply to searching a scalar for a word. Should I use grep?
You've got the regex backwards - the text should be on the left and the pattern ('Plumbing') should be on the right: $str[1] =~ /$hash{$sic}{BUS}/;
index may be a faster alternative if you're looking for simple strings. See the examples below.
Output:use strict; use warnings; my $substr = 'Plumbing'; my $string = 'ABC Plumbing'; my $pos = index( $string, $substr ); if( $pos ) { print "Found [$substr] in [$string] at $pos\n"; } else { print "[$substr] was not found in [$string]\n"; } if( $string =~ m/$substr/ ) { print "Matched [$substr] in [$string]\n"; } else { print "[$substr] was not found in [$string]\n"; }
Found [Plumbing] in [ABC Plumbing] at 4 Matched [Plumbing] in [ABC Plumbing]
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: searching a scalar for a word. Should I use grep?
by kevyt (Scribe) on Jun 23, 2007 at 04:39 UTC | |
|
Re^2: searching a scalar for a word. Should I use grep?
by kevyt (Scribe) on Jun 23, 2007 at 04:25 UTC |