I've been going crazy trying to get the right format function
I have a list that I want to write out as a comma delimited list for a
spreadsheet. I wound up with
(dolist (s l) (if s (format ot ",") (format ot "~a" s))
I spent some time trying various forms of ~:[ and ~:{ without success in
a format statement "~{~:[,~;~a~]~}" doesn't work. etc. etc.
Is there a simple single format statement for what I want. Everything I
tried wanted to do something more complicated.
--
Josh Kuperman
····@racing.saratoga.ny.us
http://racing.saratoga.ny.us/~josh (barely under contruction)
In article <·····················@josh.netheaven.com> ····@racing.saratoga.ny.us (Josh Kuperman) writes:
From: ····@racing.saratoga.ny.us (Josh Kuperman)
Date: Sat, 16 Mar 1996 18:35:55 -0500
I've been going crazy trying to get the right format function
I have a list that I want to write out as a comma delimited list for a
spreadsheet. I wound up with
(dolist (s l) (if s (format ot ",") (format ot "~a" s))
I spent some time trying various forms of ~:[ and ~:{ without success in
a format statement "~{~:[,~;~a~]~}" doesn't work. etc. etc.
Is there a simple single format statement for what I want. Everything I
tried wanted to do something more complicated.
Josh Kuperman
How about:
>(format nil "~{~A~^, ~}" '(a b c d e f))
"A, B, C, D, E, F"
--
-jeff
······@avid.com
In article <...> ····@racing.saratoga.ny.us (Josh Kuperman) writes:
>> (dolist (s l) (if s (format ot ",") (format ot "~a" s))
How about:
(format ot "~{~A~^,~}" l)
--
Thomas A. Russ, USC/Information Sciences Institute ···@isi.edu
In article <·················@hobbes.ISI.EDU>, ···@isi.edu wrote:
> In article <...> ····@racing.saratoga.ny.us (Josh Kuperman) writes:
> >> (dolist (s l) (if s (format ot "~a" s) (format ot ",") )
>
> How about:
>
> (format ot "~{~A~^,~}" l)
>
> --
> Thomas A. Russ, USC/Information Sciences Institute ···@isi.edu
Barry Margolin both answered my question and helped me to formulate it
clearly. My real problem was nils in my list, which I wanted to generated
commas but otherwise be skipped.
The format string ····@[~S~],~}" works. It prints just the comma when the
argument in the list is nil but prints all the other arguments.
--
Josh Kuperman
····@racing.saratoga.ny.us
http://racing.saratoga.ny.us/~josh (barely under contruction)
>>>>> "Josh" == Josh Kuperman <····@racing.saratoga.ny.us> writes:
Josh> I've been going crazy trying to get the right format function
Josh> I have a list that I want to write out as a comma delimited list for a
Josh> spreadsheet. I wound up with
Josh> (dolist (s l) (if s (format ot ",") (format ot "~a" s))
Josh> I spent some time trying various forms of ~:[ and ~:{ without success in
Josh> a format statement "~{~:[,~;~a~]~}" doesn't work. etc. etc.
Josh> Is there a simple single format statement for what I want. Everything I
Josh> tried wanted to do something more complicated.
Josh> --
Josh> Josh Kuperman
Josh> ····@racing.saratoga.ny.us
http> //racing.saratoga.ny.us/~josh (barely under contruction)
My solution is
USER(3): (format t "~{~S~^, ~}" '(1 2 3 4 5 6))
1, 2, 3, 4, 5, 6
If you output thousands of numbers, you will notice that the format
function is a bit slow (it interprets the format string). In that
case, you can define a faster function, like the following one:
USER(7): (defun print-my-list (list stream)
(let ((first-one? t))
(dolist (elem list)
(if first-one?
(setf first-one? nil)
(format stream ", "))
(prin1 elem stream))))
PRINT-MY-LIST
USER(8): (print-my-list '(1 2 3 4 5 6) t)
1, 2, 3, 4, 5, 6
NIL
Obviously, you should compile the function to make it run faster.
Hope this helps
Antonio Leitao
····@racing.saratoga.ny.us (Josh Kuperman) wrote:
>I've been going crazy trying to get the right format function
>I have a list that I want to write out as a comma delimited list for a
>spreadsheet. I wound up with
>(dolist (s l) (if s (format ot ",") (format ot "~a" s))
>I spent some time trying various forms of ~:[ and ~:{ without success in
>a format statement "~{~:[,~;~a~]~}" doesn't work. etc. etc.
>
>Is there a simple single format statement for what I want. Everything I
>tried wanted to do something more complicated.
>
>(dolist (s l) (if s (format ot ",") (format ot "~a" s))
You certainly didn't test this !
The following should help:
Notice the ~1:* directive that backs up 1 argument to be processed again
(format nil "~:[x~;~1:*~Ay~]" 1)
;;;=> "1Y"
(format nil "~:[x~;~1:*~Ay~]" nil)
;;;=> "X"
This is probably what is needed:
(format nil
"~{~:[~^,~;~1:*~A~^,~]~}" '(1 nil nil 2))
;;;=> "1,,,2"
Or maybe:
(format nil
"~{~:[,~;~1:*~A,~]~}" '(1 nil nil 2))
;;;=> "1,,,2,"
Francis
--
Francis Leboutte Algorithme, Rue de la Charrette 141
········@mail.interpac.be 4130 Tilff, Belgium
T&FAX: 32-(0)41-883528