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

I cannot limit each line to just one substitution. I need to make multiple substitutions on some lines of my data file.
and
How can I prevent this from happening?
are at odds with each other. Can you define in what cases you want the data to be manipulated more than once, or those cases where it should be left alone?

--MidLifeXis

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

  • Comment on Re: Algorithm to search and replace on data file, based on a table file?

Replies are listed 'Best First'.
Re^2: Algorithm to search and replace on data file, based on a table file?
by koknat (Sexton) on Sep 23, 2009 at 17:35 UTC
    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

      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.

      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