perl - Test whether one string contains any of the words in another -
i have perl program code pulls description string hash.
i have variable $ui
equal variable $uniqueid
, want return true if $ui
contains words in variable $unqiueid
. =~
isn't working.
is there smart way of doing this?
sub getdescription { $uniqueid = shift; $retval; $ui; foreach $key ( keys %hashlist ) { foreach $ui ( @{ $hashlist{$key}->{uniqueid} }) { if ( $ui eq $uniqueid ) { $retval = $hashlist{$key}->{description}; last; } } last if $retval; } return $retval; }
a simple way test if 1 scalar variable contains is:
if ($ui =~ /$uniqueid/) { ... }
(and @markus laire points out can use /\b$uniqueid\b/
prevent partial word matches.)
Comments
Post a Comment