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: Pascal Bourguignon
Subject: Re: How to parse a string?
Date: 01 May 2003 23:10:21 +0200
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3.50

Francois Fleuret <francois.fleuret@inria.fr> writes:

> Hi,
> 
> Greg Hill wrote on 01 May 2003 02:54:20 MET:
> 
> > (let ((instring "10 20 wordA wordB wordC"))
> >    (setq instring (split-string instring))
> >    (append (list (string-to-number (pop instring))
> >                        (string-to-number (pop instring)))
> >                 instring))
> 
> Thanks a lot!
> 
> But is there a generic way to do such a thing ? No scanf equivalent
> around ?
> 
> FF

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)

-- 
__Pascal_Bourguignon__                   http://www.informatimago.com/
----------------------------------------------------------------------
Do not adjust your mind, there is a fault in reality.


reply via email to

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