* bind-string-match: comments please
@ 2008-09-30 15:02 weber
2008-09-30 18:30 ` Andreas Politz
0 siblings, 1 reply; 3+ messages in thread
From: weber @ 2008-09-30 15:02 UTC (permalink / raw)
To: help-gnu-emacs
I've written this helper function:
(defun bind-string-match (regexp string &optional start)
(let ((ret (string-match regexp string start)))
(when ret
(catch 'end
(dolist (i (number-sequence 1 10))
(let ((match (match-string i string)))
(if match
(eval `(setq ,(intern (concat "$" (number-to-string i))) ,match))
(throw 'end t))))))
ret))
so that I can access the match groups with $1.. instead of (match-
string 1 string).
Is there any recommendation against this type of functions? Other than
polluting the global namespace?
Also, can one rewrite it without the `eval'?
TIA,
weber
PS: I'm not a Perl programmer!
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: bind-string-match: comments please
2008-09-30 15:02 bind-string-match: comments please weber
@ 2008-09-30 18:30 ` Andreas Politz
2008-09-30 19:22 ` weber
0 siblings, 1 reply; 3+ messages in thread
From: Andreas Politz @ 2008-09-30 18:30 UTC (permalink / raw)
To: help-gnu-emacs
weber wrote:
> I've written this helper function:
>
> (defun bind-string-match (regexp string &optional start)
> (let ((ret (string-match regexp string start)))
> (when ret
> (catch 'end
> (dolist (i (number-sequence 1 10))
> (let ((match (match-string i string)))
> (if match
> (eval `(setq ,(intern (concat "$" (number-to-string i))) ,match))
> (throw 'end t))))))
> ret))
>
> so that I can access the match groups with $1.. instead of (match-
> string 1 string).
> Is there any recommendation against this type of functions? Other than
> polluting the global namespace?
I think sooner or later you get into trouble, if you don't clear
the match variables. They could still hold values from 10 matches
ago, while the current regexp doesn't match at all.
> Also, can one rewrite it without the `eval'?
The 'q' in setq stands for quoted. So, just using set should
work.
>
> TIA,
> weber
>
> PS: I'm not a Perl programmer!
Maybe what you really want is some kind of macro ?
(defmacro with-string-matches (string &rest body)
`(let ,(mapcar (lambda (submatch)
(list (intern (format "$%d" submatch))
(match-string submatch (eval string))))
(number-sequence 0 10))
,@body))
(let ((str "abbbbc") idx)
(setq idx (string-match "a\\(b+\\)c" str))
(with-string-matches str
(message "%s %s" $0 $1)))
-ap
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: bind-string-match: comments please
2008-09-30 18:30 ` Andreas Politz
@ 2008-09-30 19:22 ` weber
0 siblings, 0 replies; 3+ messages in thread
From: weber @ 2008-09-30 19:22 UTC (permalink / raw)
To: help-gnu-emacs
On 30 set, 15:30, Andreas Politz <poli...@fh-trier.de> wrote:
> weber wrote:
> > I've written this helper function:
>
> > (defun bind-string-match (regexp string &optional start)
> > (let ((ret (string-match regexp string start)))
> > (when ret
> > (catch 'end
> > (dolist (i (number-sequence 1 10))
> > (let ((match (match-string i string)))
> > (if match
> > (eval `(setq ,(intern (concat "$" (number-to-string i))) ,match))
> > (throw 'end t))))))
> > ret))
>
> > so that I can access the match groups with $1.. instead of (match-
> > string 1 string).
> > Is there any recommendation against this type of functions? Other than
> > polluting the global namespace?
>
> I think sooner or later you get into trouble, if you don't clear
> the match variables. They could still hold values from 10 matches
> ago, while the current regexp doesn't match at all.
>
> > Also, can one rewrite it without the `eval'?
>
> The 'q' in setq stands for quoted. So, just using set should
> work.
>
>
>
> > TIA,
> > weber
>
> > PS: I'm not a Perl programmer!
>
> Maybe what you really want is some kind of macro ?
>
> (defmacro with-string-matches (string &rest body)
> `(let ,(mapcar (lambda (submatch)
> (list (intern (format "$%d" submatch))
> (match-string submatch (eval string))))
> (number-sequence 0 10))
> ,@body))
>
> (let ((str "abbbbc") idx)
> (setq idx (string-match "a\\(b+\\)c" str))
> (with-string-matches str
> (message "%s %s" $0 $1)))
>
> -ap
Yeah, I was drifting that way after recalling pg's anaphoric
macros....
Thanks a lot :)
-weber
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2008-09-30 19:22 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-09-30 15:02 bind-string-match: comments please weber
2008-09-30 18:30 ` Andreas Politz
2008-09-30 19:22 ` weber
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).