help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: [External] : Use of an associated list with completing-read


From: Heime
Subject: RE: [External] : Use of an associated list with completing-read
Date: Fri, 19 Apr 2024 03:10:26 +0000

On Friday, April 19th, 2024 at 2:59 PM, Heime <heimeborgia@protonmail.com> 
wrote:

> On Friday, April 19th, 2024 at 2:14 PM, Drew Adams drew.adams@oracle.com 
> wrote:
> 
> > > Haw can I have an interactive function that
> > > displays a list of elements from an associated
> > > list using completing-read ?
> > 
> > The elements of an alist are conses. Assuming
> > that you really meant that you want to see and
> > choose among the elements, then use this:
> > 
> > (completing-read
> > "Choose an alist element: "
> > (mapcar #'prin1-to-string my-alist)
> > nil
> > t)
> > 
> > That "displays" elements that match your
> > input. Is that what you meant by "displays
> > a list of elements"?
> > 
> > It returns a string such as "(a . 42)" for
> > the alist element (a . 42). Then you can
> > use (read "(a . 42)") to get a new cons
> > that's `equal' to the cons in your alist. ___
> 
> > But if you don't really want to see the elements of the alist as 
> > completions,
> > but instead you want to see their keys, then just do this:
> 
> > (completing-read "Choose an alist key: " my-alist nil t)
> 
> 
> Yes, I wanted to see the keys as completing entries. Right, looking
> at the documentation COLLECTION can be an alist and will print the keys.
> 
> I had the cons cells the other way round, with the actual key as the cdr,
> making completing-read display the wrong entry.
> 
> > If the alist keys are symbols then apply`intern' to the chosen string, to 
> > get the symbol key. Then use this to get the element with that key (the 
> > exact same cons, i.e.,`eq', not just` equal):
> > 
> > (car (assq THE-SYMBOL my-alist)
> > 
> > I had to do a lot of guessing as to what
> > you're really requesting. As usual, you
> > don't make clear what you're after. And
> > you don't show any code that you've tried
> > so far.

How can I get the value associated with a key.  The following does not get me 
the 
value associated with my-key. 

(let ( (lnum (cdr (rassoc my-key my-alist))))
  (when lnum (goto-line lnum))))) )




reply via email to

[Prev in Thread] Current Thread [Next in Thread]