gethash doesn't work for string keys
elisp, emacs
Solution
The default test for a hash table is `eql`. Each time you type the string, you're creating a different string, so they're not `eql` to each other.
(eql "tiger" "tiger") => nil
You need to use `equal` as the test:
(setq animals (make-hash-table :test 'equal))
Or use symbols instead of strings as the keys in your table; since symbols are interned, typing the same symbol name twice results in the `eql` objects.
Problem
Studying hash tables in elisp, I tried to write a simple example: ``` (setq animals (make-hash-table)) (puthash "tiger" 120 animals) (gethash "tiger" animals) ``` When I execute them line by line, call to `gethash` returns `nil`, despite the fact, that when I evaluate animals symbol, emacs prints this: ``` #s(hash-table size 65 test eql rehash-size 1.5 rehash-threshold 0.8 data ("tiger" 120 ...)) ``` So, "tiger" is there, but gethash doesn't return it for some reason. What's wrong? docs for hash table functions