From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Thorsten Jolitz Newsgroups: gmane.emacs.help Subject: Re: Hash tables - how to look up values? Date: Wed, 04 Jul 2012 16:21:32 +0200 Message-ID: <87y5mzbo9f.fsf@googlemail.com> References: <87d34bfwzk.fsf@kuiper.lan.informatimago.com> 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 1341411549 28026 80.91.229.3 (4 Jul 2012 14:19:09 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Wed, 4 Jul 2012 14:19:09 +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:19:08 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 1SmQQ8-00050H-Ai for geh-help-gnu-emacs@m.gmane.org; Wed, 04 Jul 2012 16:19:08 +0200 Original-Received: from localhost ([::1]:59200 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SmQQ7-0000Kj-7c for geh-help-gnu-emacs@m.gmane.org; Wed, 04 Jul 2012 10:19:07 -0400 Original-Received: from eggs.gnu.org ([208.118.235.92]:35364) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SmQPx-0000KQ-7b for help-gnu-emacs@gnu.org; Wed, 04 Jul 2012 10:19:02 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1SmQPv-0003k7-4m for help-gnu-emacs@gnu.org; Wed, 04 Jul 2012 10:18:56 -0400 Original-Received: from plane.gmane.org ([80.91.229.3]:54607) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1SmQPu-0003k0-U0 for help-gnu-emacs@gnu.org; Wed, 04 Jul 2012 10:18:55 -0400 Original-Received: from list by plane.gmane.org with local (Exim 4.69) (envelope-from ) id 1SmQPt-0004Rp-Uc for help-gnu-emacs@gnu.org; Wed, 04 Jul 2012 16:18:53 +0200 Original-Received: from e178191004.adsl.alicedsl.de ([85.178.191.4]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 04 Jul 2012 16:18:53 +0200 Original-Received: from tjolitz by e178191004.adsl.alicedsl.de with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Wed, 04 Jul 2012 16:18:53 +0200 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 52 Original-X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: e178191004.adsl.alicedsl.de User-Agent: Gnus/5.130002 (Ma Gnus v0.2) Emacs/24.0.93 (gnu/linux) Cancel-Lock: sha1:uO+/HR/jC6UPyazgE4cb8o4gHBo= X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 80.91.229.3 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:85655 Archived-At: "Pascal J. Bourguignon" writes: >> 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) Great! Thats most amazing about Emacs and its community - if you ask for a functionality that does not yet exists, it might well exist 5 minutes later just because you asked for it. Thanks a lot, exactly what I need. -- cheers, Thorsten