[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: sed problem

William James

11/7/2015 7:34:00 AM

Rob Warnock wrote:

> | 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)))

MatzLisp (Ruby):

rx = /oo/
times_to_sub = 2
while line = gets()
if (times_to_sub > 0) && rx.match(line)
times_to_sub -= 1
puts line.sub(rx, "@@")
else
puts line
end
end

testing
testing
Fool me once,
F@@l me once,
Shame on you.
Shame on you.
Fool me twice,
F@@l me twice,
Shame on me.
Shame on me.
foobar
foobar
^Z
==>nil

--
The struggle of our time is to concentrate, not to dissipate: to renew our
association with traditional wisdom: to re-establish a vital connection between
the individual and the race. It is, in a word, a struggle against Liberalism.
--- T. S. Elliot