in reply to Replacing duplicate string

This should work:
while (<FILE>) { # set modification flag to 0 my $do_mod = 0; if ($do_mod) { # if modification flag is true (>0) # apply substitution if matches s/priority/bandwidth/g; # if line is empty, clear modification flag $do_mod = 0 if (/^$/); } else { # Found first priority, mark modification flag true $do_mod++ if (/priority/); } print; }
Does that make sense?
-----
"Ask not what you can do for your country. Ask what's for lunch."
-- Orson Welles

Replies are listed 'Best First'.
Re^2: Replacing duplicate string
by lostjimmy (Chaplain) on Apr 15, 2009 at 19:07 UTC
    I don't think that code does what you think it does.
      It prints to STDOUT instead of inline editing, but that's an easy redirect. Otherwise, it works just fine on my end.
      -----
      "Ask not what you can do for your country. Ask what's for lunch."
      -- Orson Welles

        I'm sure it compiles just fine, and it does actually run, but I would have to say that you didn't actually run that code on your end, otherwise you would have seen that it doesn't run 'just fine'.

        You need to move the declaration of $do_mod outside of the loop for it to run just fine.