Dear all,
(setq beginner-alarm T)
I have a file which consists of one word per line and I would like to
read it into a single list.
Searching the web[1] I found something which I adapted to my needs.
Basically the code works but it is terribly inefficient. This is not the
fault of the CL Cookbook. Probably I am using a hammer as a screwdriver.
I'd be happy if you can give me some directions how I can tackle the
problem in a better way.
This is what I did:
(setq my-object NIL)
(with-open-file (stream "mytextfile")
(do ((line (read-line stream nil)
(read-line stream nil)))
((null line))
(setf my-object (append my-object (list line)))))
Thanks,
Roland
P.S. If you need further information: I am using currently Clisp on
WinXP at the moment.
[1] http://cl-cookbook.sourceforge.net/files.html
From: Steve Allan
Subject: Re: Reading a text file not line by line but at once (beginner)
Date:
Message-ID: <uzlnpdi52.fsf@attachmate.com>
Roland Rau <··········@gmail.com> writes:
> Dear all,
>
> (setq beginner-alarm T)
> I have a file which consists of one word per line and I would like to
> read it into a single list.
> Searching the web[1] I found something which I adapted to my
> needs. Basically the code works but it is terribly inefficient. This
> is not the fault of the CL Cookbook. Probably I am using a hammer as a
> screwdriver.
>
> I'd be happy if you can give me some directions how I can tackle the
> problem in a better way.
I like loop for this:
(with-open-file (s "foo.txt" :direction :input)
(loop for line = (read-line s nil)
while line
collect line))
--
-- Steve
On Aug 6, 3:04 pm, Steve Allan <··········@yahoo.com> wrote:
> Roland Rau <··········@gmail.com> writes:
> > Dear all,
>
> > (setq beginner-alarm T)
> > I have a file which consists of one word per line and I would like to
> > read it into a single list.
> > Searching the web[1] I found something which I adapted to my
> > needs. Basically the code works but it is terribly inefficient. This
> > is not the fault of the CL Cookbook. Probably I am using a hammer as a
> > screwdriver.
>
> > I'd be happy if you can give me some directions how I can tackle the
> > problem in a better way.
>
> I like loop for this:
>
> (with-open-file (s "foo.txt" :direction :input)
> (loop for line = (read-line s nil)
> while line
> collect line))
Ruby:
puts IO.readlines('junk')
(with-open-file (s "foo.txt" :direction :input)
(loop for line = (read-line s nil)
while line
collect line))
==>nil
On Sep 16, 11:14 am, William James <·········@yahoo.com> wrote:
> On Aug 6, 3:04 pm, Steve Allan <··········@yahoo.com> wrote:
>
>
>
> > Roland Rau <··········@gmail.com> writes:
> > > Dear all,
>
> > > (setq beginner-alarm T)
> > > I have a file which consists of one word per line and I would like to
> > > read it into a single list.
> > > Searching the web[1] I found something which I adapted to my
> > > needs. Basically the code works but it is terribly inefficient. This
> > > is not the fault of the CL Cookbook. Probably I am using a hammer as a
> > > screwdriver.
>
> > > I'd be happy if you can give me some directions how I can tackle the
> > > problem in a better way.
>
> > I like loop for this:
>
> > (with-open-file (s "foo.txt" :direction :input)
> > (loop for line = (read-line s nil)
> > while line
> > collect line))
>
> Ruby:
>
> puts IO.readlines('junk')
> (with-open-file (s "foo.txt" :direction :input)
> (loop for line = (read-line s nil)
> while line
> collect line))
> ==>nil
(defun read-lines (input)
(with-open-file (in input :direction :input)
(loop for line = (read-line in)
while line collect line)))
(compile 'read-lines)
(disassemble 'read-lines)
(read-lines "foo.txt")
Cheers
--
Marco
On Sep 16, 6:14 am, William James <·········@yahoo.com> wrote:
> Ruby:
>
> puts IO.readlines('junk')
Don't you have anything better to do? Like, masturbating?
From: Paul Donnelly
Subject: Re: Reading a text file not line by line but at once (beginner)
Date:
Message-ID: <87od2n3jm1.fsf@plap.localdomain>
namekuseijin <············@gmail.com> writes:
> On Sep 16, 6:14 am, William James <·········@yahoo.com> wrote:
>> Ruby:
>>
>> puts IO.readlines('junk')
>
> Don't you have anything better to do? Like, masturbating?
That's what he *is* doing.
From: Kaz Kylheku
Subject: Re: Reading a text file not line by line but at once (beginner)
Date:
Message-ID: <20080916142250.454@gmail.com>
On 2008-09-16, William James <·········@yahoo.com> wrote:
> On Aug 6, 3:04 pm, Steve Allan <··········@yahoo.com> wrote:
>> > I'd be happy if you can give me some directions how I can tackle the
>> > problem in a better way.
>>
>> I like loop for this:
>>
>> (with-open-file (s "foo.txt" :direction :input)
>> (loop for line = (read-line s nil)
>> while line
>> collect line))
>
> Ruby:
>
> puts IO.readlines('junk')
Your point is well made.
Lisp and Ruby each have, in their respective libraries, a single function for
reading an object from a text stream.
However, they markedly differ in what their designers consider an object.
Neither language has a single function which extract the /other's/ kind of
object from a printed representation.
It really boils down to what is considered important in the two different
programming cultures.
William James schrieb:
> On Aug 6, 3:04 pm, Steve Allan <··········@yahoo.com> wrote:
>> Roland Rau <··········@gmail.com> writes:
>>> Dear all,
>>> (setq beginner-alarm T)
>>> I have a file which consists of one word per line and I would like to
>>> read it into a single list.
>>> Searching the web[1] I found something which I adapted to my
>>> needs. Basically the code works but it is terribly inefficient. This
>>> is not the fault of the CL Cookbook. Probably I am using a hammer as a
>>> screwdriver.
>>> I'd be happy if you can give me some directions how I can tackle the
>>> problem in a better way.
>> I like loop for this:
>>
>> (with-open-file (s "foo.txt" :direction :input)
>> (loop for line = (read-line s nil)
>> while line
>> collect line))
>
> Ruby:
>
> puts IO.readlines('junk')
Better in Clojure:
slurp("file")
Andr�
--
Lisp is not dead. It�s just the URL that has changed:
http://clojure.org/
From: Alberto Riva
Subject: Re: Reading a text file not line by line but at once (beginner)
Date:
Message-ID: <g7ctjp$6g7o$1@usenet.osg.ufl.edu>
Roland Rau wrote:
> Dear all,
>
> (setq beginner-alarm T)
> I have a file which consists of one word per line and I would like to
> read it into a single list.
> Searching the web[1] I found something which I adapted to my needs.
> Basically the code works but it is terribly inefficient. This is not the
> fault of the CL Cookbook. Probably I am using a hammer as a screwdriver.
>
> I'd be happy if you can give me some directions how I can tackle the
> problem in a better way.
>
> This is what I did:
>
> (setq my-object NIL)
> (with-open-file (stream "mytextfile")
> (do ((line (read-line stream nil)
> (read-line stream nil)))
> ((null line))
> (setf my-object (append my-object (list line)))))
Using PUSH instead of APPEND is going to be much more efficient. You'll
get the lines in reverse order, so you may need to do a REVERSE or
NREVERSE at the end, but it's still going to be much more efficient.
Also, don't use SETQ, use LET.
> P.S. If you need further information: I am using currently Clisp on WinXP at the moment.
Luckily, this is not relevant at all.
Alberto
From: Roland Rau
Subject: Re: Reading a text file not line by line but at once (beginner)
Date:
Message-ID: <g7cv7p$2ni$1@gargoyle.oit.duke.edu>
Alberto Riva wrote:
> Using PUSH instead of APPEND is going to be much more efficient. You'll
> get the lines in reverse order, so you may need to do a REVERSE or
> NREVERSE at the end, but it's still going to be much more efficient.
just replacing APPEND with PUSH did not work, of course. But quickly
checking the HyperSpec, I saw how to use PUSH properly.
And what can I say: unbelievable! It works and it is fast!
Thank you very much!
The order how it is read in does not matter.
>> P.S. If you need further information: I am using currently Clisp on
>> WinXP at the moment.
>
> Luckily, this is not relevant at all.
I thought so, but being a beginner you'll never know. ;-)
Thanks again,
Roland
On Aug 6, 1:54 pm, Roland Rau <··········@gmail.com> wrote:
> Dear all,
>
> (setq beginner-alarm T)
.
.
.
You might find this link interesting:
http://www.emmett.ca/~sabetts/slurp.html