0

I have a data structure like: ((fid . FID) (name . NAME) (count . COUNT) (data . ORIGINAL_DATA))

I have following Elisp code try to interactively select FID through display the NAME instead of FID.

(defun emms-bilibili-favlist-select ()
  "Interactive select which favlist to open."
  (let* ((fav-name (completing-read "Select favlist: "
                                    (mapcar
                                     (lambda (x) (alist-get 'name x))
                                     (emms-bilibili--retrieve-favfolder))))
         ;; TODO:
         (favlist ...)
         )
    (emms-bilibili--retrieve-favlist (alist-get 'fid favlist))))

I need it to display a list in interactive select:

NAME A NAME B NAME C When I select "NAME A", code need to get the corresponding FID. (upper code is just my pseudo code, if you have better solution, please tell me, Thanks.)

2
  • Your title does not seem to represent your question. "How to interactively select alist cons pair's cdr instead of whole cons" seems to be a problem that you have successfully solved already by using alist-get instead of assq. Your actual question seems to be about mapping from the value of one alist entry to the value of another entry of the same alist. Can you please clarify the title/question to make them consistent?
    – phils
    Commented Feb 23, 2018 at 4:38
  • Sorry about this, I did considered the question title, but don't know how to describe it. I will update the title now. Commented Feb 23, 2018 at 6:47

1 Answer 1

0

If you can ensure names are unique, you can lookup the list of alist through NAME to get the corresponding FID, for example,

(let* ((alists
        '(((fid . 1) (name . "Name A"))
          ((fid . 2) (name . "Name B"))
          ((fid . 3) (name . "Name C"))))
       (name
        (completing-read
         "> "
         (mapcar
          (lambda (al) (alist-get 'name al))
          alists)))
       (alist
        (seq-find
         (lambda (al)
           (string= name (alist-get 'name al)))
         alists)))
  (alist-get 'fid alist))

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.