in reply to Re: Algorithm to search and replace on data file, based on a table file?
in thread Algorithm to search and replace on data file, based on a table file?

I'll clarify:
I want to allow multiple substitutions on each line.
I don't want the algorithm to make substitutions on data which has already been changed.

- Chris Koknat
  • Comment on Re^2: Algorithm to search and replace on data file, based on a table file?

Replies are listed 'Best First'.
Re^3: Algorithm to search and replace on data file, based on a table file?
by MidLifeXis (Monsignor) on Sep 23, 2009 at 17:44 UTC

    Now this is an interesting problem.

    One solution that I could think of right off the bat is to add a marker to the beginning and ending of the replacement string before applying it, and then not allowing the search string to operate in the area bounded by an open/close marker. Remove the markers before emitting the output.

    If you do follow this approach, watch out for greedy matching. By your definition, you should never have nested "this is changed" markers, so a minimal match on the search string should suffice.

    --MidLifeXis

    Please consider supporting my wife as she walks in the 2009 Alzheimer's Walk.

Re^3: Algorithm to search and replace on data file, based on a table file?
by NiJo (Friar) on Sep 23, 2009 at 18:17 UTC
    Your spec still has undefined behaviour regarding line ordering. What should happen with
    foo bar foo baz
    ?
      That should never happen.
      When I parse the table file, I'll need to check if a key already exists. If so, error out.

      - Chris Koknat