From: Alberto Riva
Subject: Re: How to read columns from a file?
Date: 
Message-ID: <gaei57$6g02$1@usenet.osg.ufl.edu>
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

First of all, what's the definition of 'column' here? Do you have spaces 
between the elements (a fixed number?) or tabs, or what?

> 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?

Here's one way to do it:

1. Read each line of the file with READ-LINE (don't use READ, too 
dangerous);

2. Break each line into a list of strings (there's no standard way of 
doing this in CL, but there are many functions to do that out there, and 
it's pretty easy to write your own anyway);

3. Push each element of the list in the variable corresponding to its 
column. It seems you may also want to convert some of the elements to a 
different type (string->number), you could do that on an 
element-by-element basis before the push.


Now, whether this is quick and/or easy I guess depends on a lot of 
different factors... ;)

Alberto