Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Emacs: gethash doesn't see key in hashtable

I evaluated the following elisp code in ielm:

(setq foo-hash (make-hash-table))

(puthash "location" "house" foo-hash)

(defun foo-start ()
  (interactive)
  (message (gethash "location" foo-hash)))

However when i run (foo-start) or (gethash "location" foo-hash) i get only nil echoed. Entering just foo-hash in ielm echoes: #s(hash-table size 65 test eql rehash-size 1.5 rehash-threshold 0.8 data ("location" "house"))

Is that a bug or i am doing something wrong?

Emacs version: 24.0.95.1

like image 237
Mirzhan Irkegulov Avatar asked Apr 04 '12 15:04

Mirzhan Irkegulov


1 Answers

Hash tables in elisp use eql for comparison by default. Strings won't be equal with eql unless they're the same object. You probably want to use equal, which compares the contents of the strings. Create your hash table with this:

(make-hash-table :test 'equal)
like image 120
ataylor Avatar answered Oct 29 '22 18:10

ataylor