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 strangeness Date: Sun, 02 Nov 2014 19:43:25 +0100 Organization: Informatimago Message-ID: <87k33d30ky.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: ger.gmane.org 1414954226 26242 80.91.229.3 (2 Nov 2014 18:50:26 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sun, 2 Nov 2014 18:50:26 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sun Nov 02 19:50:21 2014 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 1Xl0EF-00043Y-UH for geh-help-gnu-emacs@m.gmane.org; Sun, 02 Nov 2014 19:50:20 +0100 Original-Received: from localhost ([::1]:58913 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Xl0EF-0002zJ-Jt for geh-help-gnu-emacs@m.gmane.org; Sun, 02 Nov 2014 13:50:19 -0500 Original-Path: usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 72 Original-X-Trace: individual.net UgUYZ03hr2OCH9yoSXNlaQX99yoZ5bA3lm604Nvzqq7Yk494zi Cancel-Lock: sha1:MzFiYWE4NzgwYmNhZjhiMzUwZDFkNGE0MDQ1MzllNDI2NTdiZTkzNA== sha1:GVpDmgy4900pRmpa8bgf3F2e2MY= 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/24.3 (gnu/linux) Original-Xref: usenet.stanford.edu gnu.emacs.help:208414 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:100689 Archived-At: Tom writes: > Here's a code which gives me some headache. It's silly, because > it was shortened from a longer code to demonstrate the problem. > > It is supposed to count something in elisp functions, though > the condition is removed, so with this code the count should be > 1 for every function. > > So the code checks if the function is already in the hash > and if not then it inserts new info for that function: > > > (let ((h (make-hash-table :test 'equal))) > (mapatoms > (lambda (s) > (let* ((name (symbol-name s)) > (info (gethash name h))) > > (unless info > (setq info '(count 0))) > > (setq info (plist-put info > 'count (1+ (plist-get info 'count)))) > > (puthash name info h)))) > > (pop-to-buffer "*testout*") > (erase-buffer) > (maphash (lambda (name info) > (insert (format "%s %s" (plist-get info 'count) name) "\n")) > h)) > > > The new info is newly created in the lambda function (it is > a plist, because in the real code there are other fields too), > yet for some reason the same info structure is used for all > iterations. > > I tried to debug it and at the "(unless info" part info is nil > and then it is set to the previous value, though it should be a > new value. > > Am I missing something here? You've identified the problem with the literal list. However, you could write it much simplier: (require 'cl) ; always (let ((h (make-hash-table :test 'eql))) (mapatoms (lambda (name) (incf (getf (gethash name h) 'count 0)))) (pop-to-buffer "*testout*") (erase-buffer) (maphash (lambda (name info) (insert (format "%s %s\n" (getf info 'count) name))) h)) In the current emacs lisp, there's no point in using symbol-name strings as key in the hash-table: just use symbols with eql as test. Notice that in emacs lisp: (not (equal (symbol-name :hello) (symbol-name 'hello))) It would be different if emacs lisp had packages like Common Lisp. -- __Pascal Bourguignon__ http://www.informatimago.com/ “The factory of the future will have only two employees, a man and a dog. The man will be there to feed the dog. The dog will be there to keep the man from touching the equipment.” -- Carl Bass CEO Autodesk