Say I have a list with keywords:
'(("element1" :keyword1 "a" :keyword2 "b")
("element2" :keyword3 "c" :keyword4 "d")
("element3" :keyword2 "e" :keyword4 "f"))
Which functions can I use to find which list elements contain :keyword2 and find its value in each list? I’m trying to do this in Emacs Lisp but I think with the cl package I could possibly adapt a Common Lisp solution? I’ve tried to use the find function as illustrated here but to no avail (of course, after changing a few syntax elements to adapt the examples to Emacs Lisp).
(require 'cl) (defvar *data* '(("element1" :keyword1 "a" :keyword2 "b") ("element2" :keyword3 "c" :keyword4 "d") ("element3" :keyword2 "e" :keyword4 "f"))) (find :keyword2 *data* :test #'find) ;;=> ("element1" :keyword1 "a" :keyword2 "b") (getf (cdr (find :keyword2 *data* :test #'find)) :keyword2) ;;=> "b" ;; Above only finds the first match; to find all matches, ;; use REMOVE* to remove elements that do not contain the keyword: (remove* :keyword2 *data* :test-not #'find) ;;=> (("element1" :keyword1 "a" :keyword2 "b") ;; ("element3" :keyword2 "e" :keyword4 "f")) (mapcar (lambda (x) (getf (cdr x) :keyword2)) (remove* :keyword2 *data* :test-not #'find)) ;;=> ("b" "e")