[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Hash tables - how to look up values?
From: |
Bastien |
Subject: |
Re: Hash tables - how to look up values? |
Date: |
Wed, 04 Jul 2012 16:09:40 +0200 |
User-agent: |
Gnus/5.130006 (Ma Gnus v0.6) Emacs/24.1.50 (gnu/linux) |
Hi Thorsten,
Thorsten Jolitz <tjolitz@googlemail.com> writes:
> I would need a function that does the opposite of
>
> ,--------------------
> | (gethash key table)
> `--------------------
>
> i.e. something like
>
> ,-------------------
> | (getkey val table)
> `-------------------
>
> that returns the corresponding key of a known (string) value in a hash
> table (similar to 'rassoc' for alists).
>
> I could not find such a function - does it exist already?
I don't think so. But here is a start:
(let (key-found)
(maphash (lambda (key val)
(when (equal val "my_value")
(setq key-found key)))
hashtest)
key-found)
Of course, the shortcoming is that it stops at the first key it finds,
and discards multiple keys if several have the same value.
HTH,
--
Bastien