John Thingstad <john.thingstad@chello.no> wrote:
+---------------
| Pascal Bourguignon <pjb@informatimago.com> wrote:
| > (loop
| > :named :loop
| > :for line = (read-line *standard-input* nil nil)
| > :while line
| > :do (if (regexp:match regexp line)
| > (progn
| > (format *standard-output* "~A~%"
| > (string-substitute regexp substitution line))
| > (return-from :loop))
| > (format *standard-output* "~A~%" line)))
|
| Fine if you are happy exiting the file after the first substitution,
| but you probaly want to output the rest of the file as well.
| (not substituting)
+---------------
I was thinking exactly the same thing!! So now I guess I need
to suggest something fancier... Hmmm... How about a version
that replaces up to "n" occurrences, then outputs the rest of
the file with no further replacements? Happily, Pascal's code
can be made to do that with only a few tiny changes:
(loop
:with number-of-times-to-replace = 5
:for line = (read-line *standard-input* nil nil)
:while line
:do (if (and (plusp number-of-times-to-replace)
(regexp:match regexp line))
(progn
(decf number-of-times-to-replace)
(format *standard-output* "~A~%"
(string-substitute regexp substitution line)))
(format *standard-output* "~A~%" line)))
[And people ask me why I like Lisp...]
-Rob
-----
Rob Warnock <rpw3@rpw3.org>
627 26th Avenue <URL:http://rpw3.org/>
San Mateo, CA 94403 (650)572-2607