Subject: Re: How to read columns from a file?
From: (Rob Warnock)
Date: Sat, 13 Sep 2008 06:05:08 -0500
Newsgroups: comp.lang.lisp
Message-ID: <>
Francogrex  <> wrote:
| Hello, I have a txt file like below: test.txt (only showing a toy
| example of 3 cols and 5 rows but there may be 1000s of rows).
| jan	sun	12
| feb	mon	14
| mar	fri	23
| aug	sat	3
| jun	tue	15
| If I use this to read from file into a list:
|     (with-open-file (stream "c:/test.txt" :direction :input)
| 		    (setq *my-list* (loop for input = (read stream nil stream)
| 				  until (eq input stream) collect input)))
| I get: (JAN SUN 12 FEB MON 14 MAR FRI 23 AUG SAT 3 JUN TUE 15)
| But what I want is to directly read and store each column into one cl
| list like
| *my-list1*: (JAN FEB MAR AUG JUN), *my-list2*: (SUN MON FRI SAT TUE)
| etc..., potentially reading some columns into strings with 'readline'
| and others with 'read'.
| Is there a quick and easy way to read from a txt file each column to a
| list in CL?

Others have shown you all kinds of good stuff you might want to know
about later [when you run into more complex parsing tasks], but if
*all* you want to do is what you've stated above, and if you can always
*guarantee* that your file contains an exact multiple of three objects
readable by CL:READ (how many are on a line is irrelevant, really),
then only a very slight modification of your current code should
do the job for you:

    > (with-open-file (stream "foo")
	(loop for input = (read stream nil stream)
	      until (eq input stream)
	  collect input into first_group
	  collect (read stream) into second_group
	  collect (read stream) into third_group
	  finally (return (values first_group second_group third_group))))

    (12 14 23 3 15)

I'll leave it to you to save each of the return values wherever
you want it...


Rob Warnock			<>
627 26th Avenue			<URL:>
San Mateo, CA 94403		(650)572-2607