Francogrex <······@grex.org> 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))))
(JAN FEB MAR AUG JUN)
(SUN MON FRI SAT TUE)
(12 14 23 3 15)
>
I'll leave it to you to save each of the return values wherever
you want it...
-Rob
-----
Rob Warnock <····@rpw3.org>
627 26th Avenue <URL:http://rpw3.org/>
San Mateo, CA 94403 (650)572-2607
On Sep 13, 1:05 pm, ····@rpw3.org (Rob Warnock) wrote:
> 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))))
>
> (JAN FEB MAR AUG JUN)
> (SUN MON FRI SAT TUE)
> (12 14 23 3 15)
> >
Too verbose.
CL-USER> (series:chunk 3 3 (series:scan-file "/tmp/test.txt"))
#Z(JAN FEB MAR AUG JUN)
#Z(SUN MON FRI SAT TUE)
#Z(12 14 23 3 15)
Same caveats apply: number of columns must be known in advance, column
entries are CL:READ.
Cheers,
M/
On Sep 13, 9:33 am, Michael Weber <·········@foldr.org> wrote:
> On Sep 13, 1:05 pm, ····@rpw3.org (Rob Warnock) wrote:
>
>
>
> > 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))))
>
> > (JAN FEB MAR AUG JUN)
> > (SUN MON FRI SAT TUE)
> > (12 14 23 3 15)
> > >
>
> Too verbose.
>
> CL-USER> (series:chunk 3 3 (series:scan-file "/tmp/test.txt"))
> #Z(JAN FEB MAR AUG JUN)
> #Z(SUN MON FRI SAT TUE)
> #Z(12 14 23 3 15)
>
> Same caveats apply: number of columns must be known in advance, column
> entries are CL:READ.
>
> Cheers,
> M/
What library are you using? It looks interesting.
P� Sat, 13 Sep 2008 15:40:27 +0200, skrev Dan Weinreb <···@alum.mit.edu>:
> On Sep 13, 9:33�am, Michael Weber <·········@foldr.org> wrote:
>> On Sep 13, 1:05�pm, ····@rpw3.org (Rob Warnock) wrote:
>>
>>
>>
>> > 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))))
>>
>> > � � (JAN FEB MAR AUG JUN)
>> > � � (SUN MON FRI SAT TUE)
>> > � � (12 14 23 3 15)
>> > � � >
>>
>> Too verbose.
>>
>> CL-USER> (series:chunk 3 3 (series:scan-file "/tmp/test.txt"))
>> #Z(JAN FEB MAR AUG JUN)
>> #Z(SUN MON FRI SAT TUE)
>> #Z(12 14 23 3 15)
>>
>> Same caveats apply: number of columns must be known in advance, column
>> entries are CL:READ.
>>
>> Cheers,
>> M/
>
> What library are you using? It looks interesting.
The series libary, described in CLTH2.
http://www.cs.cmu.edu/afs/cs/project/ai-repository/ai/html/cltl/clm/node347.html#SECTION003400000000000000000
available at:
http://series.sourceforge.net/
--------------
John Thingstad