help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to parse a string?


From: Greg Hill
Subject: Re: How to parse a string?
Date: Thu, 1 May 2003 16:41:39 -0700

At 11:10 PM +0200 5/1/03, Pascal Bourguignon wrote:
Francois Fleuret <francois.fleuret@inria.fr> writes:
 > But is there a generic way to do such a thing ? No scanf equivalent
 > around ?
Just encapsulate the line with '( ... ):

    (defun scanf (string) ;; no need to specify the format and the variable,
       "We return a list of item scanned from the string."
      (read (concat "( " string " )")))

(dolist (item (scanf "10 20 3.33 wordA wordB wordC"))
    (show item (type-of item)))

==> (10 integer)
==> (20 integer)
==> (3.33 float)
==> (wordA symbol)
==> (wordB symbol)
==> (wordC symbol)


Nice trick, if you want the strings read as symbols. If you're looking for strings, how about:

(defun scanf (string)
  "We return a list of items scanned from the string."
  (mapcar (lambda (x) (if (symbolp x) (symbol-name x) x))
          (read (concat "( " string " )"))))


By the way, where is that "show" function defined? I presume it is effectively:

(defun show (&rest args) (princ "==> ") (princ args) (terpri))

--Greg




reply via email to

[Prev in Thread] Current Thread [Next in Thread]