String interpolation

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

String interpolation

Post by cormullion »

Is there a way to interpolate a value into a string? I'm thinking of an equivalent to Ruby's #{ } construct:

name = "foo"
result = "Goodnight, #{name}" #=> "Goodnight, foo"

but particularly for very long strings:

[text]
would be good
to be able to stick this >>value<<
in the middle of this string
[/text]

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

Post by Lutz »

You could do this instead:

Code: Select all

(set 'var "foo")

(replace "#var#" "Good night #var#" var)

=> "Good night foo"
Lutz

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

Post by Lutz »

Try this little function:

Code: Select all

(define-macro (repvar str)
    (dolist (_v (args)) 
        (replace (append "#" (name _v)) (eval str) (eval _v))))  

(set 'var "foo")

(repvar "Goodnight #var" var) => "Goodnight foo" 
Lutz

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

Post by cormullion »

Ha, you're too quick for me, Lutz. :-) I've just got this AppleScript interface working in response to your first answer:

(set 'applescript
[text]
set pf to POSIX file "#file#"
tell application "Finder" to set comment of pf to "#text#"
[/text])

(define (set-spotlight-comment file comment)
(replace "#file#" applescript file)
(replace "#text#" applescript comment)
(exec (string "osascript -e '" applescript "'" )))

(set-spotlight-comment "/Users/.../Documents/foo.bar" "comment for Spotlight")

(replace) is a powerful little command!

alex
Posts: 100
Joined: Thu Mar 10, 2005 2:27 pm
Location: Russia

Post by alex »

Why You can't use (format) ?

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

Post by cormullion »

You're right!

(hits head)

Locked