Re: read - comma
From: Frank Buss (fb_at_frank-buss.de)
Date: 11/09/04
- Next message: Rob Blackwell: "Re: HTTP GET from Common LISP ?"
- Previous message: David Sletten: "Re: read - comma"
- In reply to: Surendra Singhi: "Re: read - comma"
- Next in thread: Pascal Bourguignon: "Re: read - comma"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Tue, 9 Nov 2004 22:40:31 +0000 (UTC)
Surendra Singhi <efuzzyone@netscape.net> wrote:
> I wrote this piece of function:
>
> (defun read-till-delimiter (stream delimiter)
> (let (str)
> (loop for char = (read-char stream nil stream)
> until (or (eq char stream) (eq char delimiter))
> do (setf str (concatenate 'string (string char) str)))
> (nreverse str)))
>
> But I still feel it is too inefficient, especially the concatenation
> part and then reversing the string.
but if you don't need to read large files, it doesn't matter. Don't do
premature optimization.
Another short solution (perhaps not so elegant and error prone):
(with-open-file (s "c:\\tmp\\line.txt")
(loop as n = (read-delimited-list #\, s)
collect n
while (< (file-position s) (file-length s)))))
> And if you want to extract integer form the string which you have read,
> you can use:
>
> (read (make-string-input-stream "20"))
>
> This, may also be not very efficient, I am not sure.
you can use parse-integer, which is faster, but this is not so important,
but it might be important for security reasons, for example, if line.txt
contains this:
#.(+ 40 70) 2, 3, 5, 6,
my code above returns this:
((110 2) (3) (5) (6))
-- Frank Buß, fb@frank-buss.de http://www.frank-buss.de, http://www.it4-systems.de
- Next message: Rob Blackwell: "Re: HTTP GET from Common LISP ?"
- Previous message: David Sletten: "Re: read - comma"
- In reply to: Surendra Singhi: "Re: read - comma"
- Next in thread: Pascal Bourguignon: "Re: read - comma"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|