in reply to Matching Part of a String in a IF Statment
You're quoting the dollar sign of $city when you want $city to be interpolated. Perl knows when dollar is meant to mean "end-of-string" and when it's meant to mean "begin of a scalar variable". You want the following:
#!/usr/bin/perl my $city = "Naperville"; my $cityState = "Chicago, IL"; if ( $cityState =~ /\b\Q$city\E\b/) { print "They match!"; } else { print "dont match"; }
I've added \Q..\E modifiers, so you can have cities with "special" characters in their names, like []+*^ - I don't know if that's the case with your data but it's better to be safe than sorry. I also added word boundaries so Naper and Naperville are treated as two distinct towns.
|
|---|