Re: hash-table-get problem?

From: Giorgio Cesana <cesana_at_venus.sgs-thomson.it>
Date: Thu, 8 Jun 1995 08:22:13 +0200

Sarah Officer writes

] I am having difficulty retrieving a value from a hash table when the
] key is a string. It looks like the key is in the table, and I can retrieve
] it using hash-table-map or hash-table->list. Have I made a mistake?
]

Kanderso correctly answers:


] The current implemnetation of hash tables only use a test of eq?. So, you
] can only look up things that are eq?. Since
]

If you need hash table functions working on both symbol keys and strings, you
can define:

(define stk:hash-table-put! hash-table-put!)
(define stk:hash-table-get hash-table-get)
(define stk:hash-table-remove! hash-table-remove!)

(define (hash-table-put! hash key value)
        (when (string? key)
                (set! key (string->symbol key))
        (stk:hash-table-put! hash key value))

;; The default value is always initialized (to :error if no values are provided)
(define (hash-table-get hash key . default)
        (when (string? key)
                (set! key (string->symbol key))
        (if (null? default)
                (set! default :error)
                (set! default (car default)))
        (stk:hash-table-get hash key default))
        
(define (hash-table-remove! hash key)
        (stk:hash-table-remove! hash
                (if (string? key) (string->symbol key) key)))


Regards

Giorgio


-------------------------------------------------------------------------------

Giorgio Cesana | SGS-THOMSON Microelectronics
phone +39/39/603.6006 | Central R&D - DAIS
fax +39/39/603.5820 | via C. Olivetti, 2
e-mail giorgio_cesana.sgs-thomson.it | 20041 Agrate B.za (MI) - ITALY
Received on Thu Jun 08 1995 - 08:27:41 CEST

This archive was generated by hypermail 2.3.0 : Mon Jul 21 2014 - 19:38:59 CEST