I need to generate a space-padded string with a variable string length. The not-so-clever solution that works involved nesting of format
:
(format nil (format nil "~~~d,,a" 10) "asdf")
Now, I wanted to make it a bit more clever by using format
's ~?
for recursive processing. I would expect that something like this should do what I want:
(format nil "~@?" "~~~d,,a" 10 "asdf")
but what I get is just the formatting string, i.e. ~10,,a
, not the padded asdf
string. Perhaps I misunderstood the word 'recursive' here, but I would expect that having formed the inner format string, CL should proceed to actually use it. Am I missing something?
v
in the control string:(format nil "~va" 10 "asdf")
– Giglio(format nil "~10,,,'-a" "asdf")
. Any chance of achieving it using~?
?? – Cylindroid(format nil "~v,,,va" 10 #\- "asdf")
– Otherworldlyv
s:(format nil "~v,,,va" 10 #\- "asdf")
. You misunderstand the use case for~?
. It's mainly meant for inserting another "call" to format with a different control string and arguments in a control string. For example, to prompt for a yes or no, you might have a function like(defun y-or-n-prompt (control-string &rest args) (format t "~&~? [y/n]: " control-string args))
– Giglioformat
also processes this substring. – CylindroidFORMAT
at run time is a bad idea. It's error prone and if you use a constant control string, implementations may process it at compile time rather than run time (producing more efficient code). – Giglio