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

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

Re: How modify numbers in a region by a multiplier?


From: Qiang Guo
Subject: Re: How modify numbers in a region by a multiplier?
Date: Thu, 01 Jul 2010 10:54:31 -0600
User-agent: Wanderlust/2.15.9 (Almost Unreal) SEMI/1.14.6 (Maruoka) FLIM/1.14.9 (Gojō) APEL/10.7 Emacs/23.2 (x86_64-apple-darwin10.4.0) MULE/6.0 (HANACHIRUSATO)

At Thu, 1 Jul 2010 13:55:11 +0000 (UTC),
Seweryn Kokot wrote:
> 
> 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)))
> 
> 



Hi,

The problem is the re-search-forward. It should be
dynamically changed according to the replace events, since
the (point) would exceed the variable end after replacement.

Qiang 



reply via email to

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