Subject: Re: With format, how to print a variable number of spaces
From: rpw3@rpw3.org (Rob Warnock)
Date: Mon, 05 Jun 2006 02:41:37 -0500
Newsgroups: comp.lang.lisp
Message-ID: <IuidnXozJ52sfR7ZnZ2dnUVZ_t-dnZ2d@speakeasy.net>
ramza2 <berlin.brown@gmail.com> wrote:
+---------------
| How can I print a variable number of spaces.  In python I might do:
|   print " " * 3
| How would I use 'format' to do something similar.
+---------------

There are several ways. It depends on what you're really trying to do.
If you want to tabulate to a specific column position then "~T" with
a variable parameter ("~VT") will do that:

    > (loop for i below 20
	    for col = (+ 32 (round (* 8 (sin (* i pi 1/4))))) do
	(format t "~&Let's tab from here to~vtHERE.~%" col))
    Let's tab from here to          HERE.
    Let's tab from here to                HERE.
    Let's tab from here to                  HERE.
    Let's tab from here to                HERE.
    Let's tab from here to          HERE.
    Let's tab from here to    HERE.
    Let's tab from here to  HERE.
    Let's tab from here to    HERE.
    Let's tab from here to          HERE.
    Let's tab from here to                HERE.
    Let's tab from here to                  HERE.
    Let's tab from here to                HERE.
    Let's tab from here to          HERE.
    Let's tab from here to    HERE.
    Let's tab from here to  HERE.
    Let's tab from here to    HERE.
    Let's tab from here to          HERE.
    Let's tab from here to                HERE.
    Let's tab from here to                  HERE.
    Let's tab from here to                HERE.
    NIL
    > 

If you're trying to do variable widths with things other than spaces,
you can use the "mincol" & "padchar" options of "~A":

    > (loop for i below 20
	    for col = (+ 20 (round (* 8 (sin (* i pi 1/4))))) do
	(format t "~&Let's draw from here to ~V,,,'-@A~%" col "> here"))

    Let's draw from here to --------------> here
    Let's draw from here to --------------------> here
    Let's draw from here to ----------------------> here
    Let's draw from here to --------------------> here
    Let's draw from here to --------------> here
    Let's draw from here to --------> here
    Let's draw from here to ------> here
    Let's draw from here to --------> here
    Let's draw from here to --------------> here
    Let's draw from here to --------------------> here
    Let's draw from here to ----------------------> here
    Let's draw from here to --------------------> here
    Let's draw from here to --------------> here
    Let's draw from here to --------> here
    Let's draw from here to ------> here
    Let's draw from here to --------> here
    Let's draw from here to --------------> here
    Let's draw from here to --------------------> here
    Let's draw from here to ----------------------> here
    Let's draw from here to --------------------> here
    NIL
    > 

For more complicated stuff, SUBSEQ can be useful:

    > (defparameter *digits* (apply 'concatenate 'string
			            (loop for i below 10
				      collect "0123456789")))
    > (loop for i below 20
	    for col = (+ 20 (round (* 8 (sin (* i pi 1/4))))) do
	(format t "~&~A = ~32T~D chars~%" (subseq *digits* 0 col) col))

    01234567890123456789 =          20 chars
    01234567890123456789012345 =    26 chars
    0123456789012345678901234567 =  28 chars
    01234567890123456789012345 =    26 chars
    01234567890123456789 =          20 chars
    01234567890123 =                14 chars
    012345678901 =                  12 chars
    01234567890123 =                14 chars
    01234567890123456789 =          20 chars
    01234567890123456789012345 =    26 chars
    0123456789012345678901234567 =  28 chars
    01234567890123456789012345 =    26 chars
    01234567890123456789 =          20 chars
    01234567890123 =                14 chars
    012345678901 =                  12 chars
    01234567890123 =                14 chars
    01234567890123456789 =          20 chars
    01234567890123456789012345 =    26 chars
    0123456789012345678901234567 =  28 chars
    01234567890123456789012345 =    26 chars
    NIL
    > 


-Rob

-----
Rob Warnock			<rpw3@rpw3.org>
627 26th Avenue			<URL:http://rpw3.org/>
San Mateo, CA 94403		(650)572-2607