From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Greg Hill Newsgroups: gmane.emacs.help Subject: Re: How to parse a string? Date: Thu, 1 May 2003 11:02:24 -0700 Sender: help-gnu-emacs-bounces+gnu-help-gnu-emacs=m.gmane.org@gnu.org Message-ID: References: <87ade7d023.fsf@noos.fr> NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" ; format="flowed" X-Trace: main.gmane.org 1051812248 26941 80.91.224.249 (1 May 2003 18:04:08 GMT) X-Complaints-To: usenet@main.gmane.org NNTP-Posting-Date: Thu, 1 May 2003 18:04:08 +0000 (UTC) Original-X-From: help-gnu-emacs-bounces+gnu-help-gnu-emacs=m.gmane.org@gnu.org Thu May 01 20:04:05 2003 Return-path: Original-Received: from monty-python.gnu.org ([199.232.76.173]) by main.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 19BIPd-00070L-00 for ; Thu, 01 May 2003 20:04:05 +0200 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.10.13) id 19BIPa-0005tC-09 for gnu-help-gnu-emacs@m.gmane.org; Thu, 01 May 2003 14:04:02 -0400 Original-Received: from list by monty-python.gnu.org with tmda-scanned (Exim 4.10.13) id 19BIPC-0005F1-00 for help-gnu-emacs@gnu.org; Thu, 01 May 2003 14:03:38 -0400 Original-Received: from mail by monty-python.gnu.org with spam-scanned (Exim 4.10.13) id 19BIOk-0004GF-00 for help-gnu-emacs@gnu.org; Thu, 01 May 2003 14:03:13 -0400 Original-Received: from renfield.synergymicro.com ([153.105.4.30] helo=synergymicro.com) by monty-python.gnu.org with esmtp (Exim 4.10.13) id 19BIOh-0004A7-00 for help-gnu-emacs@gnu.org; Thu, 01 May 2003 14:03:07 -0400 Original-Received: from synergy.synergy.encinitas.ca.us ([153.105.4.29]) by synergymicro.com (8.9.3/8.9.3) with ESMTP id LAA30421; Thu, 1 May 2003 11:04:31 -0700 Original-Received: from [198.17.100.22] (G_Hill_Mac [198.17.100.22]) h41IAPoJ028869; Thu, 1 May 2003 11:10:25 -0700 In-Reply-To: Original-To: Francois Fleuret , help-gnu-emacs@gnu.org X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1b5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Help: List-Post: List-Subscribe: , List-Archive: List-Unsubscribe: , Errors-To: help-gnu-emacs-bounces+gnu-help-gnu-emacs=m.gmane.org@gnu.org Xref: main.gmane.org gmane.emacs.help:9131 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:9131 At 11:56 AM +0200 5/1/03, Francois Fleuret wrote: >But is there a generic way to do such a thing ? No scanf equivalent >around ? Not as standard Emacs Lisp, but no doubt someone has written their own. I'm surprised nobody has replied by giving you a web page where you could find one. It wouldn't be hard to write one for yourself, and then you can make it behave any way you like. Here is something quick and dirty I just hacked together to demonstrate what I mean. It takes a "format string" as the first argument, and the string to be parsed as the second. Each character in the format string tells how to translate one "word" in the string to be parsed, where: i = integer f = float n = number (integer or float, don't care) s = string anything else = skip (defun scanf (format string) (let ((list nil) fmt word) (setq string (split-string string) format (split-string format "")) (while (and string format) (setq word (pop string) fmt (pop format)) (cond ((equal fmt "i") (setq word (floor (string-to-number word)))) ((equal fmt "f") (setq word (float (string-to-number word)))) ((equal fmt "n") (setq word (string-to-number word))) ((not (equal fmt "s")) (setq word nil))) (if word (setq list (append list (list word))))) list)) Use it like this: (scanf "ifsxnn" "1 2.3 word skip 4 5.6") ==> (1 2.3 "word" 4 5.6) Now go ahead and be a real Emacser. Cook up your own function work any way you like. --Greg