Path: csiph.com!usenet.pasdenom.info!gegeweb.org!aioe.org!.POSTED!not-for-mail From: Kaz Kylheku Newsgroups: comp.programming Subject: Re: Suggest a command line syntax Date: Mon, 22 Dec 2014 21:40:22 +0000 (UTC) Organization: Aioe.org NNTP Server Lines: 47 Message-ID: <20141222133135.43@kylheku.com> References: NNTP-Posting-Host: X+c6YNb3AaWMPA3YfA4opg.user.speranza.aioe.org X-Complaints-To: abuse@aioe.org User-Agent: slrn/pre1.0.0-18 (Linux) X-Notice: Filtered by postfilter v. 0.8.2 Xref: csiph.com comp.programming:5031 On 2014-12-22, Robert Wessel wrote: > On Mon, 22 Dec 2014 20:10:06 +0200, Victor Porton > wrote: > >>I am writing a program (in fact a Perl script), which should do replacement >>of strings in a file, like this: >> >>AB->BC >>BC->AB >>YY->ZZ >> >>Note that Sed and relatives fail to do this task: It would replace BC back >>with AB after replacing AB with BC, and this is not what I need. > > > Sure it will, you just need to do it a bit differently. Do the > following: > > change AB -> QQQQ > change BC -> AB > change YY-> ZZ > change QQQQ -> AB > > If "QQQQ" conflicts with another string in your source, use a > different temporary string in the first and fourth steps. This can be made "air tight" if it includes an extra pass which detects whether the rename string like QQQQ occurs in the file. while not long enough (temp_string_list ) do do t = generate_unique_temp_string(); while (t occurs in input file or t occurs in replacement strings) push t onto temp_string_list done After this, you have enough required temp strings. Note that you never want the temp strings to actually have a form like QQQQ. This is because QQQQ has a suffix and a prefix which can combine to create a spuriouis QQQQ. If the original text contains no QQQQ, but contains QQAB, after an AB -> QQQQ a substitution you have QQQQQQ, oops! A brace-delimited token like {QQQQ} would be better. An insertion of {QQQQ} cannot combine with fragments of {QQQQ} to produce a spurious {QQQQ}.