Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
The idea is that if the hash key is "Fish", I want to find all lines containing "sFish_val" before replacing this string with "my_temp_var". But instead of printing the lines containing the sub-string defined in $pattern, the code prints every single line containing the letter 's'. Please Help!!! Thanks, Timothyforeach $line (@file) { chomp($line); foreach $key (keys %hash) { $pattern = "s$key_val"; if($line =~ /$pattern/) { print "$line\n"; $line =~ s/$pattern/my_temp_var/; } } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Variables in regex pattern match give strange results.
by Zaxo (Archbishop) on Jan 24, 2002 at 10:54 UTC | |
|
Re: Variables in regex pattern match give strange results.
by demerphq (Chancellor) on Jan 24, 2002 at 14:34 UTC |