Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problem with hash tables: gethash does not return values

41 views
Skip to first unread message

frvallee

unread,
Nov 9, 2011, 10:42:49 AM11/9/11
to
Hello lispers,

I have a problem with a hash table in my program.
- I declare a hash table as a slot of a class:

(defclass Environment ()
((parameters :accessor parameters :type Hash-
table :initarg :parameters
:initform (make-hash-table))))

- Then I use a function called fill-hash-table

(defun fill-hash-table (ht pair-list)
"Fill the hashtable 'ht' with values of 'pair-list'. 'Pair-list' is
a list such as: '((key1 value1)
(key2 value2))"
(mapc (lambda (item)
(setf (gethash (first item) ht) (second item)))
pair-list))

- I set this hash table with values as follows:

(fill-hash-table (parameters environment)
(list (list 'epsilon-mat (make-sym-mat epsilon-mat))
(list 'sigma-mat (make-sym-mat sigma-mat))
'(temperature 310)
'(R 8.3143e20)
'(mu 7e-14)
'(NAV 6.0221415e23)))))

The problem comes when I want to use the hash table because gethash
returns NIL everytime. The strange is when I want to see what is in
the table with maphash it works !
Is there a problem with the test ?
Or are my keys not symbols ?

Alex Mizrahi

unread,
Nov 9, 2011, 4:19:01 PM11/9/11
to
> The problem comes when I want to use the hash table because gethash
> returns NIL everytime. The strange is when I want to see what is in
> the table with maphash it works !
> Is there a problem with the test ?
> Or are my keys not symbols ?

I guess problem is in the code with calls gethash


Pascal J. Bourguignon

unread,
Nov 9, 2011, 4:34:27 PM11/9/11
to
It works perfectly:


CL-USER> (defclass Environment ()
((parameters :accessor parameters :type Hash-table :initarg :parameters
:initform (make-hash-table))))

#<STANDARD-CLASS ENVIRONMENT>
CL-USER> (defun fill-hash-table (ht pair-list)
"Fill the hashtable 'ht' with values of 'pair-list'. 'Pair-list' is
a list such as: '((key1 value1) (key2 value2))"
(mapc (lambda (item)
(setf (gethash (first item) ht) (second item)))
pair-list))
FILL-HASH-TABLE
CL-USER> (apropos "PRINT-HASHTABLE")
COM.INFORMATIMAGO.COMMON-LISP.CESARUM.UTILITY:PRINT-HASHTABLE, Def: FUNCTION
; No value
CL-USER> (let ((environment (make-instance 'environment)))
(fill-hash-table (parameters environment)
;; quoting undefined stuff:
(list (list 'epsilon-mat '(make-sym-mat epsilon-mat))
(list 'sigma-mat '(make-sym-mat sigma-mat))
'(temperature 310)
'(R 8.3143e20)
'(mu 7e-14)
'(NAV 6.0221415e23)))
(COM.INFORMATIMAGO.COMMON-LISP.CESARUM.UTILITY:PRINT-HASHTABLE (parameters environment)))
#.(HASHTABLE :TEST (FUNCTION EQL) :SIZE 6
:REHASH-SIZE 1.5 :REHASH-THRESHOLD 0.85
:ELEMENTS '(
(SIGMA-MAT (MAKE-SYM-MAT SIGMA-MAT))
(NAV 6.0221414E+23)
(EPSILON-MAT (MAKE-SYM-MAT EPSILON-MAT))
(R 8.3143E+20)
(TEMPERATURE 310)
(MU 7.0E-14)))
#<HASH-TABLE :TEST EQL size 6/60 #x302001B847FD>
CL-USER>


--
__Pascal Bourguignon__ http://www.informatimago.com/
A bad day in () is better than a good day in {}.

Kaz Kylheku

unread,
Nov 9, 2011, 8:46:34 PM11/9/11
to
On 2011-11-09, frvallee <frva...@gmail.com> wrote:
> The problem comes when I want to use the hash table because gethash
> returns NIL everytime. The strange is when I want to see what is in
> the table with maphash it works !
> Is there a problem with the test ?
> Or are my keys not symbols ?

Pascal reported that the code you postd works perfectly. This means that the
problem is somewhere else.

Are you sure you are pulling the hash table from the correct CLOS instance?

Maybe your code is tripped up somewhere: perhaps you are putting values into
one hash table and confirming that one with maphash, but perhaps elsewhere you
are doing gethash on a different table, thinking it is the same instance.

Jonathan Smith

unread,
Nov 10, 2011, 3:57:16 PM11/10/11
to
Hi,

Is it possible that you are in two different packages when you fill
the hash-table and when you attempt to retrieve values from it?

Same-name symbols from two different packages would be different keys
in a hash-table.

An easy way to test this would be to use a package-qualified symbol at
both ends of this.
example:

(fill-hash-table (parameters environment)
                     (list (list 'cl-user::epsilon-mat (make-sym-mat
epsilon-mat))
                           (list 'sigma-mat (make-sym-mat sigma-mat))
                           '(temperature 310)
                           '(R 8.3143e20)
                           '(mu 7e-14)
                           '(NAV 6.0221415e23)))))

(gethash 'cl-user::epsilon-mat hashtable)

frvallee

unread,
Nov 14, 2011, 4:09:50 AM11/14/11
to
The problem does not come from the package names although I fill and
reuse the hash table in different packages.
As I explained, I have access to the hash table. In the function where
I want to use it I do :

- (describe parameters)

which print

#<HASH-TABLE :TEST EQL :COUNT 6 {1005643BF1}>
[hash-table]

Occupancy: 0.4
Rehash-threshold: 1.0
Rehash-size: 1.5
Size: 16
Synchronized: no

- (maphash (lambda (k v) (format t "~a/~a~%" k v)) parameters)

which print

EPSILON-MAT/#<SYM-MATRIX {10029C3221}>
SIGMA-MAT/#<SYM-MATRIX {10029C3241}>
TEMPERATURE/310
R/8.3143e20
MU/7.e-14
NAV/6.0221414e23

- and (format t "~s~%" (gethash 'epsilon-mat parameters))

which print

NIL

I really don't understand.

Alex Mizrahi

unread,
Nov 14, 2011, 4:56:36 AM11/14/11
to
> The problem does not come from the package names although I fill and
> reuse the hash table in different packages.

How do you know? It's a likely cause.

> - (maphash (lambda (k v) (format t "~a/~a~%" k v)) parameters)

> which print

> EPSILON-MAT/#<SYM-MATRIX {10029C3221}>

~A format directive does not print package prefix. Use ~S or use PRINT.

frvallee

unread,
Nov 14, 2011, 5:06:00 AM11/14/11
to
Apologies. After testing the solution of Jonathan, it is actually a
problem of package.

Many thanks !
0 new messages