I am pretty new to string processing in Mathematica. But I'm having some problems in doing simple string template filling to my mind elegantly. For example when doing export for my answer here, one of my solutions so far is using:
cat = StringJoin @@ (ToString /@ {##}) &;
strvec = cat[#[[1]], " ", #[[2]], " ", #[[3]]] &;
StringJoin[Map[cat["AttributeBegin
Translate ", strvec[#[[3]]], "
Color ", strvec[#[[2]]], "
Surface \"constant\"
Sphere 0.1 -0.1 0.1 360.0
Attribute \"light\" \"shadows\" \"on\"
LightSource \"pointlight\" \"l", #[[1]], "\"
\"intensity\" 1
\"lightcolor\" [", strvec[#[[2]]], "]
AttributeEnd
Illuminate \"l", #[[1]], "\" 1
"] &, {{1, Blue, {0, 0, -1}}, {1, Red, {3, 1.5, 1}}}]]
But what I would really like to do is have a separate the template string from the code so its easy to change without touching the rest of the code. Ideally id want something like this:
lightTemplate = "AttributeBegin
Translate $trans$
Color $color$
Surface \"constant\"
Sphere 0.1 -0.1 0.1 360.0
Attribute \"light\" \"shadows\" \"on\"
LightSource \"pointlight\" \"l$num$\"
\"intensity\" 1
\"lightcolor\" [ $color$ ]
AttributeEnd
Illuminate \"$num$\"
"
Then somehow map my database to fill in values to $num$
, $color$
etc... Like i would do in mostly any other high level language (python uses % or {} instead of $ but the idea is the same). I tried using StringFormat
but that didn't work too well.
In a related note is there a nice way to turn something like:
RGBColor[1, 0, 0]
to
"1 0 0"
ToString@Row[{##}, " "] & @@ RGBColor[1, 0, 0]
$\endgroup$