in reply to Problems with matching?

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; } }

Replies are listed 'Best First'.
Re^2: Problems with matching?
by Argel (Prior) on May 18, 2011 at 23:56 UTC
    THANKS!!! I knew I was overlooking something obvious but just couldn't see it!!

    Update1: Thanks for the second snippet, but the device names appear at least twice. And I need to use the full names because sometimes the 4 character sequence will match something unrelated in the config file. Didn't think scrubbing this data would be such a pain! :/

    Update2: Yep, I did miss it! Thanks for the help!!

    Elda Taluta; Sarks Sark; Ark Arks

      And I need to use the full names

      I used the full names. You might have missed my change to smarter capturing.

      You: / ... (\w\w\w\w)(-\w+-\w+) ... / Me: / ... (\w\w\w\w(-\w+-\w+)) ... /