3

I've spent a good amount of hours trying to solve this one but without success. I have an alist with strings

(setq trees '(("pine" . "cones") ("oak" . "acorns") ("maple" . "seeds")))

and I want to obtain the assoc of it using a regexp as key. That is

(assoc ".ine" trees)

always returns nil instead of ("pine" . "cones"). I've tried to use the assoc-default function instead, using string-match but it also did not work. I know that this is pretty basic, but I cannot really understand what I am doing wrong. Thank you in advance.

2
  • emacs.stackexchange.com/tags/elisp/info
    – Drew
    Commented Aug 18, 2021 at 17:18
  • It's acceptable. It just shouldn't have tag elisp. Your question is about using Elisp to accomplish something; it's not a question about the nature of the language (e.g. compared to other Lisps).
    – Drew
    Commented Aug 18, 2021 at 17:30

1 Answer 1

4

Pass a TESTFN arg to assoc. It needs to use string-match-p with the args reversed. That is, assoc passes TESTFN the pattern arg second, but string-match-p expects the pattern as its first arg.

(assoc ".*ine" trees (lambda (x regexp) (string-match-p regexp x)))

I filed doc bug report #50110 for this, asking that the arg order for optional arg TESTFN be specified. And that bug has now been fixed!

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.