The functions write-hash-table and read-hash-table write
and read a hash table to and from a port respectively.
;; write-hash-table: hash-table port -> void
(define (write-hash-tabletableport)
(parameterize ((print-hash-table#t))
(writetableport)))
;; read-hash-table port -> (U hash-table #f)
(define (read-hash-tableport)
(let ((data (readport)))
(if (hash-table?data)
data#f)))
Note that read-hash-table returns #f if the data it
reads from the port is not a hash-table. As a side-effect
the data read is thrown away. If you don't want this data
to be thrown away simply call read on the port, as
read-hash-table is just a wrapper around read.
PltScheme includes built-in support for writing and reading
hash tables. However there are some caveats:
Hash tables are only written when the parameter
print-hash-table is true. This parameter is false by
default, so all write-hash-table does is temporarily
set the parameter to true and call write.
The hash tables read by read and hence
read-hash-table are immutable. That is, they cannot
be modified. If you want to create a mutable hash
table use HashCopy to copy the immutable hash table
into a mutable one.