From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: pjb@informatimago.com (Pascal J. Bourguignon) Newsgroups: gmane.emacs.help Subject: Re: How modify numbers in a region by a multiplier? Date: Thu, 01 Jul 2010 18:45:55 +0200 Organization: Informatimago Message-ID: <87sk43xi9o.fsf@kuiper.lan.informatimago.com> References: <87pqz7fb8o.fsf@fh-trier.de> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: dough.gmane.org 1291842128 32481 80.91.229.12 (8 Dec 2010 21:02:08 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Wed, 8 Dec 2010 21:02:08 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Dec 08 22:02:04 2010 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1PQR9I-0002VB-IQ for geh-help-gnu-emacs@m.gmane.org; Wed, 08 Dec 2010 22:02:04 +0100 Original-Received: from localhost ([127.0.0.1]:37202 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1PQR26-0000B2-GL for geh-help-gnu-emacs@m.gmane.org; Wed, 08 Dec 2010 15:54:38 -0500 Original-Path: usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 50 Original-X-Trace: individual.net QmjhEAznrXtZ5+oCMZxFjgbS2+ticItipRz1ioOkjC5gqvF0II Cancel-Lock: sha1:YTUzZWExZGIxZTQzMTA4MWE4MmEzOTAzYzQ1ZDc4Y2UwZTFjZWM3NQ== sha1:bBexc27UZwjQuoec+pRmUUExq1Y= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en X-Disabled: X-No-Archive: no User-Agent: Gnus/5.101 (Gnus v5.10.10) Emacs/23.2 (gnu/linux) Original-Xref: usenet.stanford.edu gnu.emacs.help:179419 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:76000 Archived-At: Andreas Politz writes: > Seweryn Kokot writes: > >> Hi, >> >> I would like to modify numbers in a region/buffer by a given multiplier. >> >> Imagine that I have some numbers >> >> 33.444 3333 4433.4443 3344 .34234 >> >> and I want them multiplied for example by 0.1 >> >> to receive >> >> 3.3444 333.3 443.34443 334.4 .034234 >> >> I tried with this function I wrote, but it doesn't work properly. >> Any idea why? >> >> (defun my-multiply-numbers-in-region-or-buffer (multiplier) >> (interactive "nGive multiplier: ") >> (let (beg end object) >> (if (use-region-p) >> (progn >> (setq object "region") >> (setq beg (region-beginning)) >> (setq end (region-end))) >> (setq object "buffer") >> (setq beg (point-min)) >> (setq end (point-max))) >> (goto-char beg) >> (while (re-search-forward "\\([0-9]*\\.?[0-9]*\\)" end t) >> (replace-match (format "%.3f" (* (string-to-number (match-string 1)) >> multiplier)))) >> (message "Numbers in %s modified by multiplier %s." object >> multiplier))) > > The problem is that your regexp matches the empty word. Or said otherwise: Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems. -- Jamie Zawinski -- __Pascal Bourguignon__ http://www.informatimago.com/