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

I am trying to search a scalar for a word that is in another scalar and I can't seem to get it to work. I found other postings but I could not get anything to work. For example. If the first variable has "Plumbing" and a second variable has "ABC Plumbing", I want to know if the variable holding "ABC Plumbing" has the word "Plumbing" in it. I have something like this but it is not working.
$sic = 1711; $hash{$sic}{BUS} = 'Plumbing'; # A keyword in a business name $hash{$sic}{CAT} = 'Plumbing'; # Category $str[1] = 'Elite Plumbing'; foreach $sic (keys %hash){ # if the word in $hash{$sic}{BUS} is found in $str[1], # I want to know. # if ($hash{$sic}{BUS} =~ /$str[1]/) # This wont work. if (grep { $hash{$sic}{BUS} == $_ } $str[1] ){ # Nope # $str[38] = $hash{$sic}{CAT}; # $str[39] = $hash{$sic}{BUS}; print "Found one. Bus name is $str[1] and cat is " . $hash{$sic} +{CAT} . " and sic is " . $str[39] ."\n"; } }
I know that it has to be simple but I can't figure it out. Can someone tell me what I am missing? Since I have posted this, I noticed that I am using grep wrong. Thanks Kevin

Replies are listed 'Best First'.
Re: searching a scalar for a word. Should I use grep?
by bobf (Monsignor) on Jun 23, 2007 at 04:12 UTC

    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]

      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
      Wow... thanks !!!! This will work !!! Have a great weekend :) Kevin
Re: searching a scalar for a word. Should I use grep?
by GrandFather (Saint) on Jun 23, 2007 at 04:34 UTC
    use strict; use warnings; my %hash = (1711 => {BUS => 'Plumbing', CAT => 'Plumbing'}); my @str = ('Elite Plumbing', 'Wonderfull Welding'); for my $key (keys %hash) { my @matches = grep {-1 < index $_, $hash{$key}{BUS}} @str; next unless @matches; print "Match for $hash{$key}{BUS} in:\n "; print join "\n ", @matches; }

    Prints:

    Match for Plumbing in: Elite Plumbing

    DWIM is Perl's answer to Gödel