From: ab talebi
Subject: ReadLine in Lisp?
Date: 
Message-ID: <3bd16c18.5911936@news.online.no>
Do we have a ReadLine function in Lisp which reads one line at a time
from a file and stores its value in variables?

the point of doing this is that we have a file which looks like this:

a	b c d e
f	g h
i	j k l

and we would like to have variables called "first-line" "second-line"
etc. each variable containing the value of the line so that the
variable "first-line" would contain the list 
(a 	b c d e)
there are two tricky parts here: 
1) we do not know how many lines we have in the file
2) the input is not in list

tnx
ab talebi

From: Steve Long
Subject: Re: ReadLine in Lisp?
Date: 
Message-ID: <3BD1A583.A3B0B719@isomedia.com>
Dude,

A lot of people think you are looking for homework assistance. If that
is true, read the following...

If you explicitly tell us that you are doing this for a homework
assignment (like one might find at the University of Oslo) this
newsgroup can appropriately answer your questions. I believe that most
ng participants adhere to a comp.lang.lisp Prime Directive as concerns
writing code for others: The do not think that just giving a solution to
a student is in anyone's best interest, especially yours. In the long
run, a little -- rather than a lot of -- help will help the student
learn how to learn -- kind of like an innoculation.

I suggest a book called "Common Lisp: A Gentle Introduction to Symbolic
Computation" by Touretzky.

Khoda'Hafez

ab talebi wrote:

> Do we have a ReadLine function in Lisp which reads one line at a time
> from a file and stores its value in variables?
>
> the point of doing this is that we have a file which looks like this:
>
> a       b c d e
> f       g h
> i       j k l
>
> and we would like to have variables called "first-line" "second-line"
> etc. each variable containing the value of the line so that the
> variable "first-line" would contain the list
> (a      b c d e)
> there are two tricky parts here:
> 1) we do not know how many lines we have in the file
> 2) the input is not in list
>
> tnx
> ab talebi
From: Kent M Pitman
Subject: Re: ReadLine in Lisp?
Date: 
Message-ID: <sfwofn2pcyf.fsf@world.std.com>
············@yahoo.com (ab talebi) writes:

> Do we have a ReadLine function in Lisp which reads one line at a time
> from a file and stores its value in variables?

There is a function READ-LINE.

It reads from a character stream, not a file.  A character stream is a
series of characters that may or may not come from a file.  See the 
function OPEN and the macro WITH-OPEN-FILE.  Both are documented in CLHS.

READ-LINE doesn't store the line it reads anywhere.  It returns the line
as a value.  It's up to you to put it in a variable if that's what you want.

To read from standard input:

 (SETQ LINE (READ-LINE))

To read from a stream other than standard input:

 (SETQ LINE (READ-LINE INPUT-STREAM))

> the point of doing this is that we have a file which looks like this:
> 
> a	b c d e
> f	g h
> i	j k l
> 
> and we would like to have variables called "first-line" "second-line"
> etc. each variable containing the value of the line so that the
> variable "first-line" would contain the list 
> (a 	b c d e)
> there are two tricky parts here: 
> 1) we do not know how many lines we have in the file

So you might need values like FOUR-HUNDRED-THIRTY-SECOND-LINE?  
Honestly, I don't think you want to do this with variables.
I think this is either a badly writen homework assignment or one
you didn't repeat verbatim.

If it IS homework, and it certainly smells of it, you should say so.

If it is not homework, you should not add constraints like this.

> 2) the input is not in list

This doesn't mean you can't accumulate it into a list.
From: Edward Fagan
Subject: Re: ReadLine in Lisp?
Date: 
Message-ID: <ey3n12kdlx6.fsf@dom.ain>
> Do we have a ReadLine function in Lisp which reads one line at a time
> from a file and stores its value in variables?

> the point of doing this is that we have a file which looks like this:

> a	b c d e
> f	g h
> i	j k l

> and we would like to have variables called "first-line" "second-line"
> etc. each variable containing the value of the line so that the
> variable "first-line" would contain the list 
> (a 	b c d e)
> there are two tricky parts here: 
> 1) we do not know how many lines we have in the file
> 2) the input is not in list

This is very easy.

(defvar *current-line* 0)
(defun read-a-line (file)
  (with-open-file (in file :direction ':input)
    (loop repeat *current-line*
          for r = (read-line in nil nil)
          if (null r) do (return-from read-a-line nil)
          finally (progn
                    (incf *current-line*)
                    (return-from read-a-line 
                      (read-line in nil nil))))))

(defun get-lines-from-file (file)
  (setf *current-line* 0)
  (loop for l = (read-a-line file)
        while l
        do (set (intern (string-upcase
                         (format nil "~:R-LINE" *current-line*)))
                l)))
                                
From: ab talebi
Subject: Re: ReadLine in Lisp?
Date: 
Message-ID: <3bd406a5.5772703@news.uio.no>
On 22 Oct 2001 11:11:17 +0100, Edward Fagan <··@dom.ain> wrote:

>This is very easy.
>
>(defvar *current-line* 0)
>(defun read-a-line (file)
>  (with-open-file (in file :direction ':input)
>    (loop repeat *current-line*
>          for r = (read-line in nil nil)
>          if (null r) do (return-from read-a-line nil)
>          finally (progn
>                    (incf *current-line*)
>                    (return-from read-a-line 
>                      (read-line in nil nil))))))
>
>(defun get-lines-from-file (file)
>  (setf *current-line* 0)
>  (loop for l = (read-a-line file)
>        while l
>        do (set (intern (string-upcase
>                         (format nil "~:R-LINE" *current-line*)))
>                l)))
>                                

yes, actually if you want to get the first line as a string it's much
easier that this! but the point was to get the first line as a list.
something like:
(a	b c d e)

ab taebi
From: Kalle Olavi Niemitalo
Subject: Re: ReadLine in Lisp?
Date: 
Message-ID: <iznwv1n6es4.fsf@stekt34.oulu.fi>
············@yahoo.com (ab talebi) writes:

> but the point was to get the first line as a list.
> something like:
> (a	b c d e)

I hope you realize that list is the same as (a b c d e).
Lisp ignores the amount of whitespace between list elements.
From: Coby Beck
Subject: Re: ReadLine in Lisp?
Date: 
Message-ID: <lXVA7.442949$8c3.77532669@typhoon.tampabay.rr.com>
"Edward Fagan" <··@dom.ain> wrote in message ····················@dom.ain...
> > Do we have a ReadLine function in Lisp which reads one line at a time
> > from a file and stores its value in variables?
>
> > the point of doing this is that we have a file which looks like this:
>
> > a b c d e
> > f g h
> > i j k l
>
> > and we would like to have variables called "first-line" "second-line"
> > etc. each variable containing the value of the line so that the
> > variable "first-line" would contain the list
> > (a b c d e)
> > there are two tricky parts here:
> > 1) we do not know how many lines we have in the file
> > 2) the input is not in list
>
> This is very easy.
>
> (defvar *current-line* 0)
> (defun read-a-line (file)
>   (with-open-file (in file :direction ':input)
>     (loop repeat *current-line*
>           for r = (read-line in nil nil)
>           if (null r) do (return-from read-a-line nil)
>           finally (progn
>                     (incf *current-line*)
>                     (return-from read-a-line
>                       (read-line in nil nil))))))
>
> (defun get-lines-from-file (file)
>   (setf *current-line* 0)
>   (loop for l = (read-a-line file)
>         while l
>         do (set (intern (string-upcase
>                          (format nil "~:R-LINE" *current-line*)))
>                 l)))
>

(a couple of comments:  why put with-open-file inside read-a-line to open and
close your file every loop in get-lines-from-file?  And why use a special for
*current-line* when you could just pass it to read-a-line? Why did you quote
the keyword :input, I've never seen that before...?)

As long as we are proposing solutions for ab's homework, here is an example of
a more "lispy" way to do this.  It does not create and bind variables as
invisible side-effects (not only that, it would be a real challenge to find out
how many and what symbols you have interned) but rather returns a nested list.
(hopefully this can demonstrate a few lisp basics but it doesn't quite meet the
stated requirements)

(defun get-letters (line)
  (let ((list-of-letters '()) (position 0) (letter nil))
    (loop
     (multiple-value-setq (letter position)
         (read-from-string line nil nil :start position))
     (if (null letter)
         (return (reverse list-of-letters))
       (push letter list-of-letters)))))

(defun parse-alpha-file (name)
  (with-open-file (input name :direction :input)
    (loop for line = (read-line input nil nil)
          until (null line)
          collect (get-letters line))))

Coby

--
(remove #\space "coby . beck @ opentechgroup . com")
From: Tim Bradshaw
Subject: Re: ReadLine in Lisp?
Date: 
Message-ID: <ey3itd7emis.fsf@cley.com>
* Coby Beck wrote:

> (a couple of comments:  why put with-open-file inside read-a-line to open and
> close your file every loop in get-lines-from-file?  And why use a special for
> *current-line* when you could just pass it to read-a-line? Why did you quote
> the keyword :input, I've never seen that before...?)

One of the things I noticed when reading this is that it's quadratic
in file length.  It may be that some of the other `interesting'
properties such as non-reentrancy (via *CURRENT-LINE*) &c are somehow
related to this.

I do quoted keywords too though, if I'm using a keyword in a position
where it's just a value, not a keyword indicator in a function call.
It's similar to '(), which I also do.

--tim
From: Janis Dzerins
Subject: Re: ReadLine in Lisp?
Date: 
Message-ID: <87hesrllky.fsf@asaka.latnet.lv>
Tim Bradshaw <···@cley.com> writes:

> I do quoted keywords too though, if I'm using a keyword in a
> position where it's just a value, not a keyword indicator in a
> function call.  It's similar to '(), which I also do.

I don't quote keywords (but I don't get confused when others do,
anymore). And I don't quote nil and empty list either (my reasoning:
quoted objects are immutable so I don't quote empty list if it's used
for value accumulation with push).

-- 
Janis Dzerins

  Eat shit -- billions of flies can't be wrong.
From: Tim Bradshaw
Subject: Re: ReadLine in Lisp?
Date: 
Message-ID: <ey3elnveieq.fsf@cley.com>
* Janis Dzerins wrote:

> I don't quote keywords (but I don't get confused when others do,
> anymore). And I don't quote nil and empty list either (my reasoning:
> quoted objects are immutable so I don't quote empty list if it's used
> for value accumulation with push).

I'm sure you know this, but PUSH doesn't mutate the empty list.
From: Janis Dzerins
Subject: Re: ReadLine in Lisp?
Date: 
Message-ID: <877ktnliga.fsf@asaka.latnet.lv>
Tim Bradshaw <···@cley.com> writes:

> * Janis Dzerins wrote:
> 
> > I don't quote keywords (but I don't get confused when others do,
> > anymore). And I don't quote nil and empty list either (my reasoning:
> > quoted objects are immutable so I don't quote empty list if it's used
> > for value accumulation with push).
> 
> I'm sure you know this, but PUSH doesn't mutate the empty list.

Yes, I know it. And keywords are self-evaluating...

-- 
Janis Dzerins

  Eat shit -- billions of flies can't be wrong.
From: Tim Bradshaw
Subject: Re: ReadLine in Lisp?
Date: 
Message-ID: <ey31yjvefsj.fsf@cley.com>
* Janis Dzerins wrote:
> Yes, I know it. And keywords are self-evaluating...

Touch�!

--tim