Non-destructive replace?

Notices and updates
Locked
cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Non-destructive replace?

Post by cormullion »

How do I a non-destructive replace? I've realised that my problem is:

(set 'new-filename (replace "." old-file-name "copy."))

- I've destroyed the old-file-name...

It seems wrong to make a safe copy beforehand.

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

Just wrap the old filename with anything which returns the filename:

Code: Select all

> (set 'old "myfile.txt")
"myfile.txt"
> (replace "." (string old) ".copy.")
"myfile.copy.txt"
> old
"myfile.txt"
> 
even 'if', 'or', 'and' or 'begin' would work, but 'string' is more descriptive. Or using implicit 'slice'

Code: Select all

(replace "." (0 old) ".copy.")

Lutz

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

(replace "." (string old) ".copy.")
That's clever - thanks!

Fanda
Posts: 253
Joined: Tue Aug 02, 2005 6:40 am
Contact:

Post by Fanda »

Instead of a (string old) you could also use:

Code: Select all

(set 'old)  or  (setq old)
Fanda

Locked