The search you start with the match op is completed with the substitution op because they both use the same iterator (pos($file)). As a result, your match op always starts from the start of $file.
my %translations; my %scrubbed; while ($file =~ /^ \s+ description \s (\w\w\w\w(-\w+-\w+)) ;/xmg) { my $real_dev = $1; my $name = $2; if (!$translation{$real_dev}) { # Make sure not to duplicate anything in %scrubbed. my $scrubbed_dev = ...; ++$scrubbed{$scrubbed_dev}; $translation{$real_dev} = $scrubbed_dev; } } my $pat = join '|', map quotemeta, keys %translations; my $re = qr/$pat/; $file =~ s/($re)/$translations{$1}/g;
If you just want to change the device description instead of every occurrence, I suggest
my $output = ''; my %translations; my %scrubbed; for my $line (split(//m, $file, -1)) { if ( my ($pre, $real_dev, $name, $post) = $line =~ /^ ( \s+ description \s ) (\w\w\w\w(-\w+-\w+)) ( ; . +* ) /xs) { ) { my $scrubbed_dev = $translation{$real_dev}; if (!$scrubbed_dev) { # Make sure not to duplicate anything in %scrubbed. $scrubbed_dev = ...; ++$scrubbed{$scrubbed_dev}; $translation{$real_dev} = $scrubbed_dev; } $output .= $pre . $scrubbed_dev . $post; } else { $output .= $line; } }
In reply to Re: Problems with matching?
by ikegami
in thread Problems with matching?
by Argel
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |