[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How modify numbers in a region by a multiplier?
From: |
Pascal J. Bourguignon |
Subject: |
Re: How modify numbers in a region by a multiplier? |
Date: |
Wed, 08 Dec 2010 15:16:52 -0000 |
User-agent: |
Gnus/5.101 (Gnus v5.10.10) Emacs/23.2 (gnu/linux) |
Andreas Politz <politza@fh-trier.de> writes:
> 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.
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/
- Re: How modify numbers in a region by a multiplier?, David Kastrup, 2010/12/08
- Re: How modify numbers in a region by a multiplier?, Marc Mientki, 2010/12/08
- Re: How modify numbers in a region by a multiplier?, Andreas Politz, 2010/12/08
- Re: How modify numbers in a region by a multiplier?,
Pascal J. Bourguignon <=
- Re: How modify numbers in a region by a multiplier?, Pascal J. Bourguignon, 2010/12/08
- Re: How modify numbers in a region by a multiplier?, David Kastrup, 2010/12/08
- Re: How modify numbers in a region by a multiplier?, Andreas Politz, 2010/12/08
- Re: How modify numbers in a region by a multiplier?, David Kastrup, 2010/12/08
- Re: How modify numbers in a region by a multiplier?, David Kastrup, 2010/12/08