From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: Klaus Berndl Newsgroups: gmane.emacs.help Subject: Re: alist and multiple values for one key Date: Mon, 20 Jan 2003 17:07:01 +0100 Sender: help-gnu-emacs-bounces+gnu-help-gnu-emacs=m.gmane.org@gnu.org Message-ID: References: NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable X-Trace: main.gmane.org 1043080568 18386 80.91.224.249 (20 Jan 2003 16:36:08 GMT) X-Complaints-To: usenet@main.gmane.org NNTP-Posting-Date: Mon, 20 Jan 2003 16:36:08 +0000 (UTC) Return-path: Original-Received: from monty-python.gnu.org ([199.232.76.173]) by main.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 18aeu1-0004ld-00 for ; Mon, 20 Jan 2003 17:36:02 +0100 Original-Received: from localhost ([127.0.0.1] helo=monty-python.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.10.13) id 18aefH-0001TT-0C for gnu-help-gnu-emacs@m.gmane.org; Mon, 20 Jan 2003 11:20:47 -0500 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!news-spur1.maxwell.syr.edu!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.fjserv.net!oleane.net!oleane!bnewspeer00.bru.ops.eu.uu.net!DE21.cocreate.com!dnewsfeed01.dtm.ops.eu.uu.net!dnewsifeed00.dtm.ops.eu.uu.net!bnewsifeed02.bru.ops.eu.uu.net!bnewspost00.bru.ops.eu.uu.net!emea.uu.net!read.news.de.uu.net!not-for-mail Original-NNTP-Posting-Host: access.sdm.de Original-Newsgroups: gnu.emacs.help Original-Lines: 59 X-Newsreader: Microsoft (R) Exchange Internet News Service Version 5.5.2653.11 Original-Xref: shelby.stanford.edu gnu.emacs.help:109232 Original-To: help-gnu-emacs@gnu.org X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1b5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Help: List-Post: List-Subscribe: , List-Archive: List-Unsubscribe: , Errors-To: help-gnu-emacs-bounces+gnu-help-gnu-emacs=m.gmane.org@gnu.org Xref: main.gmane.org gmane.emacs.help:5756 X-Report-Spam: http://spam.gmane.org/gmane.emacs.help:5756 On 20 Jan 2003, Norbert C. wrote: > Hi, > =20 > I would lile to find the "good way" to retrieve multiple values > for a given key in an alist. > =20 > But I didn't find a natural way to do it. For example : > (setq trees '((pine . cones) (pine . acorns) (oak . acorns) (maple . > seeds))) > =3D=3D> ((pine . cones) (pine . acorns) (oak . acorns) (maple . = seeds)) > (assoc 'pine trees) > =3D=3D> (pine . cones) > =20 > What I'd like is something that returns each value associated with > 'pine. > =20 > Any thought ? Multimaps are unfortunately not build in in Emacs. But you can = implement it very easy. Here is a first fast hack how you can do it, probably not robust and fancy enough but it gives you a first impression: ,---- | ;; a fast hack for a multimap | (defvar multimap nil) |=20 | (defun multimap-add (key value) | (let ((elem (assoc key multimap))) | (if elem | (when (not (member value (cdr elem))) | (setcdr elem (append (list value) (cdr elem)))) | (setq multimap (cons (list key value) multimap))) | multimap)) |=20 | (defun multimap-get (key) | (interactive "sKey: ") | (message "%s" (cdr (assoc key multimap)))) |=20 |=20 | ;; some tests | (multimap-add "key1" "value1-1") | (multimap-add "key1" "value1-2") | (multimap-add "key2" "value2-1") | (multimap-add "key2" "value2-2") | (multimap-add "key2" "value2-3") | (multimap-add "key3" "value3-3") `---- Klaus --=20 Klaus Berndl mailto: klaus.berndl@sdm.de sd&m AG http://www.sdm.de software design & management Thomas-Dehler-Str. 27, 81737 M=FCnchen, Germany Tel +49 89 63812-392, Fax -220