From: John Hunter
Subject: simple lisp question
Date: 
Message-ID: <1rwveyqcnd.fsf@video.bsd.uchicago.edu>
I would like a function that takes a filename as an arg and returns each line
(separated by newline) in an alist whose car is the string of text on
that line, and a complementary function which prints the car of the
alist to the file.

I have read some of the emacs lisp info manual on this subject, but
the solution is not immediately apparent.  For example, with '(read BUFFER)' how do
you specify the buffer name?  How do you read in a line at a time
until the end of the file?  

I thought this would be a relatively common thing to do and someone
might already have some template functions which I could follow.

Thanks,
John Hunter 
From: Barry Margolin
Subject: Re: simple lisp question
Date: 
Message-ID: <6BlJ5.30$6w5.414@burlma1-snr2>
In article <··············@video.bsd.uchicago.edu>,
John Hunter  <········@nitace.bsd.uchicago.edu> wrote:
>I would like a function that takes a filename as an arg and returns each line
>(separated by newline) in an alist whose car is the string of text on
>that line, and a complementary function which prints the car of the
>alist to the file.

Here's how to do it in Common Lisp.

(defun file-to-alist (filename)
  (with-open-file (s filename)
    (loop for line = (read-line s)
          while line
          collect (cons line nil))))

(defun alist-to-file (filename alist)
  (with-open-file (s filename :direction :output)
    (dolist (item alist)
      (write-string (car item) s))))

>I have read some of the emacs lisp info manual on this subject, but
>the solution is not immediately apparent.  For example, with '(read
>BUFFER)' how do
>you specify the buffer name?  How do you read in a line at a time
>until the end of the file?  

READ isn't used for reading lines, it's used for reading the printed
representation of a Lisp object.  To get a buffer with a particular name,
use (get-buffer "name").  So to read from a specific buffer, use something
like:

(let ((buffer (get-buffer "name")))
  (read buffer))

-- 
Barry Margolin, ······@genuity.net
Genuity, Burlington, MA
*** DON'T SEND TECHNICAL QUESTIONS DIRECTLY TO ME, post them to newsgroups.
Please DON'T copy followups to me -- I'll assume it wasn't posted to the group.