From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Pascal J. Bourguignon" Newsgroups: gmane.emacs.help Subject: Re: Hash tables - how to look up values? Date: Wed, 04 Jul 2012 15:59:27 +0200 Organization: Informatimago Message-ID: <87d34bfwzk.fsf@kuiper.lan.informatimago.com> References: NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: dough.gmane.org 1341410415 18752 80.91.229.3 (4 Jul 2012 14:00:15 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Wed, 4 Jul 2012 14:00:15 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Wed Jul 04 16:00:14 2012 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1SmQ7p-0006zV-6V for geh-help-gnu-emacs@m.gmane.org; Wed, 04 Jul 2012 16:00:13 +0200 Original-Received: from localhost ([::1]:51297 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SmQ7o-0003bC-9L for geh-help-gnu-emacs@m.gmane.org; Wed, 04 Jul 2012 10:00:12 -0400 Original-Path: usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 63 Original-X-Trace: individual.net I74b43B6mfpjUfLb4ZLQZg8qpxtfAF+cDcMdnGVEZW99xYlMT0huPA14VGLawd/7g0 Cancel-Lock: sha1:OWY0NGQ1MWI4OGY5YzMxYTljYTRkZDU3ZDAyMDRlMmQ5ZTg0NzJiZQ== sha1:15AybpNS+6L7RIf2/ntSNcB7bp0= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.4 (gnu/linux) Original-Xref: usenet.stanford.edu gnu.emacs.help:193263 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:85652 Archived-At: Thorsten Jolitz writes: > Hi List, > 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? rassoc returns the first hit, because lists, even association lists are ordered. Hash tables are not ordered. We can write something similar, returning the first hit, but there's no guarantee the first will always be the same. (require 'cl) (defun* getkey (val table &key (test (function eql)) (default nil)) ;; (hash-table-test table) is for keys, not for values… (maphash (lambda (k v) (when (funcall test val v) (return-from getkey k))) table) default) (let ((h (make-hash-table))) (setf (gethash :one h) "un" (gethash :two h) "deux" (gethash :a h) "un") (getkey "un" h :test (function string=))) --> :one ; or :a, who knows? I'd rather write: (defun* getkeys (val table &key (test (function eql))) ;; (hash-table-test table) is for keys, not for values… (let ((keys '())) (maphash (lambda (k v) (when (funcall test val v) (push k keys))) table) keys)) (let ((h (make-hash-table))) (setf (gethash :one h) "un" (gethash :two h) "deux" (gethash :a h) "un") (getkeys "un" h :test (function string=))) --> (:a :one) -- __Pascal Bourguignon__ http://www.informatimago.com/ A bad day in () is better than a good day in {}.