Subject: Re: With format, how to print a variable number of spaces
From: rpw3@rpw3.org (Rob Warnock)
Date: Tue, 06 Jun 2006 21:14:42 -0500
Newsgroups: comp.lang.lisp
Message-ID: <zNadnf8lGcwPqxvZnZ2dnUVZ_qadnZ2d@speakeasy.net>
Peter Seibel  <peter@gigamonkeys.com> wrote:
+---------------
| ocorrain@yahoo.com (Tiarn�n � Corr�in) writes:
| > ...say I have a list of key/value pairs, in an alist:
| >        '((foo 3) (bar 2) (baz 10))
| > Can I use format with the list iteration operators to get output like:
| >     "foo=3;bar=3;baz=10;"?
| 
|   CL-USER> (format t "~{~{~(~a~)=~a~};~}" '((foo 3) (bar 2) (baz 10)))
|   foo=3;bar=2;baz=10;
|   NIL
+---------------

You only need one iteration level if you use the colon modifier:

    > (format t "~:{~(~a~)=~a;~}" '((foo 3) (bar 2) (baz 10)))
    foo=3;bar=2;baz=10;
    NIL
    > 

+---------------
| And if, as is often the case, you don't want the list separator (the
| #\; in this case) after the last item, you can use this:
| 
|   CL-USER> (format t "~{~{~(~a~)=~a~}~^;~}" '((foo 3) (bar 2) (baz 10)))
|   foo=3;bar=2;baz=10
|   NIL
+---------------

Ditto, mutatis mutandis:

    > (format t "~:{~(~a~)=~a~:^;~}" '((foo 3) (bar 2) (baz 10)))
    foo=3;bar=2;baz=10
    NIL
    > 


-Rob

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