Re: read - comma

From: Frank Buss (fb_at_frank-buss.de)
Date: 11/09/04


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


Relevant Pages

  • Re: syntax...
    ... B&D on the part of the language designer. ... probably handle concatenation of string literals by itself, ... bitwise XOR, or if not that, then exponentiation.) ...
    (comp.lang.misc)
  • Bugs in the Module::Dependency
    ... Manifying blib/man1/pmd_indexer.plx.1 ... Use of uninitialized value in concatenation or string at ... # Failed test in t/04grapher.t at line 81. ...
    (perl.dbi.users)
  • RE: Simple regex problem has me baffled
    ... I am writing a script to analyse a log file. ... Each pair of Request and Response calls have a unique ... Use of uninitialized value in concatenation or string at ...
    (perl.beginners)
  • Re: Literal concatenation, strings vs. numbers (was: Numeric literals in other than base 10 - wa
    ... Well my take on it is that this would not be the same as string ... concatenation, the series of digits would be parsed as a single token ... digit separation can superficially resemble string concatenation if ... I would favor _ as a digit separator in Python ...
    (comp.lang.python)
  • Re: Simple regex problem has me baffled
    ... I am writing a script to analyse a log file. ... Each pair of Request and Response calls have a unique ... Use of uninitialized value in concatenation or string at ./magic.pl ...
    (perl.beginners)