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: Pascal J. Bourguignon
Subject: Re: How modify numbers in a region by a multiplier?
Date: Wed, 08 Dec 2010 15:16:54 -0000
User-agent: Gnus/5.101 (Gnus v5.10.10) Emacs/23.2 (gnu/linux)

Qiang Guo <mcknight0219@gmail.com> writes:

> 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.

Obviously NOT!

The point is a marker, not an integer.


(with-temp-buffer 
  (insert "aaa-bbb-ccc")
  (goto-char 0)
  (when (re-search-forward "\\(b+\\)")
    (replace-match "xxxxxx")
    (insert "|"))
  (buffer-substring (point-min) (point-max)))

--> "aaa-xxxxxx|-ccc"



-- 
__Pascal Bourguignon__                     http://www.informatimago.com/


reply via email to

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