newLISP Fan Club

Forum => Anything else we might add? => Topic started by: cormullion on December 03, 2005, 06:29:09 AM

Title: String interpolation
Post by: cormullion on December 03, 2005, 06:29:09 AM
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]
Title:
Post by: Lutz on December 03, 2005, 06:36:06 AM
You could do this instead:

(set 'var "foo")

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

=> "Good night foo"


Lutz
Title:
Post by: Lutz on December 03, 2005, 06:46:55 AM
Try this little function:

(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
Title:
Post by: cormullion on December 03, 2005, 07:03:28 AM
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!
Title:
Post by: alex on December 05, 2005, 09:37:22 PM
Why You can't use (format) ?
Title:
Post by: cormullion on December 06, 2005, 12:05:44 AM
You're right!



(hits head)