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: Andreas Politz
Subject: Re: How modify numbers in a region by a multiplier?
Date: Wed, 08 Dec 2010 15:16:48 -0000
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.50 (gnu/linux)

Seweryn Kokot <sewkokot@gmail.com> 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.

-ap


reply via email to

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