Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: How to print/output array in a tab-delimited format?

206 views
Skip to first unread message
Message has been deleted

Pascal J. Bourguignon

unread,
Jul 14, 2008, 5:48:49 AM7/14/08
to
Francogrex <fra...@grex.org> writes:

> I create an array and print it to a file as such:
>
> (setf arr #2A((1 2 3) (4 5 6) (7 8 9)))
> (setf out-stream (open "Output.txt" :direction :output))
> (print arr out-stream)
> (close out-stream)

Don't use OPEN. Use WITH-OPEN-FILE:

(with-open-file (out-stream "Ouput.txt :direction :output)
(print arr out-stream))

> But the output is in the CL array form. Is there a way to print the
> array's columns and rows in a tab-delimited format that can be
> immediadely read as a spreadsheet by something like MS-Excel?

Yes. Writing programs. There's no magic behind the magic, only the
stuff of the trade. You're in to code, so go ahead, code!


> Thanks

Contrarily to popular belief, there is a big number (line in one, two,
three, big) of CSV lisp libraries. Choose and use one of them.

For example:
http://darcs.informatimago.com/darcs/public/lisp/common-lisp/csv.lisp
(Notice that *writing* CSV involves only two functions, so it's hardly
worth a library, which may explain why there are so many of them).


However, you will have to convert it to lists, since FORMAT ~{~}
accepts only lists:

(loop
:with element-type = (array-element-type arr)
:with record-size = (array-dimension arr 1)
:with record = (make-list record-size)
:for i :from 0 :below (array-dimension arr 0)
:for row = (make-array record-size
:displaced-to arr
:displaced-index-offset (* i record-size)
:element-type element-type)
:do (replace record row)
(com.informatimago.common-lisp.csv:write-record record out-stream))

--
__Pascal Bourguignon__ http://www.informatimago.com/

"Remember, Information is not knowledge; Knowledge is not Wisdom;
Wisdom is not truth; Truth is not beauty; Beauty is not love;
Love is not music; Music is the best." -- Frank Zappa

John Thingstad

unread,
Jul 15, 2008, 10:07:58 AM7/15/08
to
På Mon, 14 Jul 2008 10:41:55 +0200, skrev Francogrex <fra...@grex.org>:

> I create an array and print it to a file as such:
>
> (setf arr #2A((1 2 3) (4 5 6) (7 8 9)))
> (setf out-stream (open "Output.txt" :direction :output))
> (print arr out-stream)
> (close out-stream)
>

> But the output is in the CL array form. Is there a way to print the
> array's columns and rows in a tab-delimited format that can be

> immediadely read as a spreadsheet by something like MS-Excel? Thanks

use format.
~{ ~} are used to iterate over a list. Do a (coerce array 'list) first.
~< ~> are used for tabulated entries.
Also look up ~T ~; ~& ~% ~D ~F

--------------
John Thingstad

Marco Antoniotti

unread,
Jul 15, 2008, 10:27:22 AM7/15/08
to
On Jul 15, 4:07 pm, "John Thingstad" <jpth...@online.no> wrote:
> På Mon, 14 Jul 2008 10:41:55 +0200, skrev Francogrex <fra...@grex.org>:
>
> > I create an array and print it to a file as such:
>
> > (setf arr #2A((1 2 3) (4 5 6) (7 8 9)))
> > (setf out-stream (open "Output.txt" :direction :output))
> > (print arr out-stream)
> > (close out-stream)
>
> > But the output is in the CL array form. Is there a way to print the
> > array's columns and rows in a tab-delimited format that can be
> > immediadely read as a spreadsheet by something like MS-Excel? Thanks
>
> use format.
> ~{ ~} are used to iterate over a list. Do a (coerce array 'list) first.

You cannot (at least directly). You can COERCE to a LIST only VECTORs
(or other SEQUENCEs). Pascal J.B. used the displacing trick but YMMV.

Cheers
--
Marco

0 new messages