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.

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"; }
Output:
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
    Wow... thanks a bunch!!!! This worked !!!
    Thanks for your very clear example.
    I placed the prints on 2 - 3 lines for readability.
    foreach $sic (keys %hash){ next if !$hash{$sic}{BUS}; print "str1 = $str[1] sic= $sic, cat= $hash{$sic}{CAT} bus= $hash{$sic}{BUS}\n"; if ( $str[1] =~ m/$hash{$sic}{BUS}/){ $str[38] = $hash{$sic}{CAT}; $str[39] = $sic; print "Found one. Bus name is $str[1] and cat is " . $hash{$sic}{CAT} . " and sic is " . $str[39] ."\n\n\n"; } }

    output:
    str1 = 'Domino''s Pizza' sic= 5812, cat= Pizza restaurants bus= Pizza Found one. Bus name is 'Domino''s Pizza' and cat is Pizza restaurants + and sic is 5812

    Have a great weekend :)
    Kevin
Re^2: searching a scalar for a word. Should I use grep?
by kevyt (Scribe) on Jun 23, 2007 at 04:25 UTC
    Wow... thanks !!!! This will work !!! Have a great weekend :) Kevin