all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* How to replace-regexp by the average?
@ 2015-06-05 20:30 gnuist006
  2015-06-05 21:12 ` John Mastro
  0 siblings, 1 reply; 2+ messages in thread
From: gnuist006 @ 2015-06-05 20:30 UTC (permalink / raw)
  To: help-gnu-emacs

Given strings of this type

TOKEN 123.456 12.3456 1234.56

replace by

TOKEN 67.9008 623.453

where the first number is the average, ie what you get from the evaluation of

(/ (+ 123.456 12.3456) 2.0)

and second is also the average, ditto

(/ (+ 1234.56 12.3456) 2.0)

both upto 6 significant digits.

I tried this but it does not work.

(save-excursion (replace-regexp "TOKEN \\([0-9\\.]+\\) \\([0-9\\.]+\\) \\([0-9\\.]+\\)" (concat "TOKEN (format "6.6f" (/ (+ \\1 \\2) 2.0)) (format "6.6f" (/ (+ \\2 \\3) 2.0)) ) ))

Any help in improving while keeping it readable one-liner sexp and maintaining the use of \\1 \\2 \\3 etc if possible ... ?

Cheers,
Bolega


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: How to replace-regexp by the average?
  2015-06-05 20:30 How to replace-regexp by the average? gnuist006
@ 2015-06-05 21:12 ` John Mastro
  0 siblings, 0 replies; 2+ messages in thread
From: John Mastro @ 2015-06-05 21:12 UTC (permalink / raw)
  To: gnuist006, help-gnu-emacs@gnu.org

<gnuist006@gmail.com> wrote:
> Given strings of this type
>
> TOKEN 123.456 12.3456 1234.56
>
> replace by
>
> TOKEN 67.9008 623.453
>
> where the first number is the average, ie what you get from the
> evaluation of
>
> (/ (+ 123.456 12.3456) 2.0)
>
> and second is also the average, ditto
>
> (/ (+ 1234.56 12.3456) 2.0)
>
> both upto 6 significant digits.
>
> I tried this but it does not work.
>
> (save-excursion (replace-regexp "TOKEN \\([0-9\\.]+\\) \\([0-9\\.]+\\) \\([0-9\\.]+\\)" (concat "TOKEN (format "6.6f" (/ (+ \\1 \\2) 2.0)) (format "6.6f" (/ (+ \\2 \\3) 2.0)) ) ))
>
> Any help in improving while keeping it readable one-liner sexp and
> maintaining the use of \\1 \\2 \\3 etc if possible ... ?

You can only use Lisp expressions in the replacement text for
`replace-regexp' in interactive calls (i.e. when used via `M-x' rather
than `M-:'). The syntax is also different from what you tried, for which
see `C-h f replace-regexp RET`.

Here's one stab at what it might look like in Lisp. This doesn't meet
all your requirements (it's not a one-liner and doesn't pay attention to
significant digits), and it's probably not the best way to do it, but
maybe it will give you some ideas.

    (defun replace-with-averages ()
      (interactive)
      (save-excursion
        (when (re-search-forward
               "TOKEN \\([0-9.]+\\) \\([0-9.]+\\) \\([0-9.]+\\)"
               nil t)
          (let ((i (string-to-number (match-string 1)))
                (j (string-to-number (match-string 2)))
                (k (string-to-number (match-string 3))))
            (replace-match (format "TOKEN %s %s"
                                   (/ (+ i j) 2.0)
                                   (/ (+ j k) 2.0)))))))

-- 
john



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-06-05 21:12 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-06-05 20:30 How to replace-regexp by the average? gnuist006
2015-06-05 21:12 ` John Mastro

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.