Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
writing/printing hash-tables to a file
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  2 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Erik Naggum  
View profile  
 More options Jun 28 1998, 3:00 am
Newsgroups: comp.lang.lisp
From: Erik Naggum <cle...@naggum.no>
Date: 1998/06/28
Subject: Re: writing/printing hash-tables to a file

* kp gores
| how do i "print" a hash-table to a file?

  sometimes, it works to print the hash-table object with *PRINT-READABLY*
  true.  when that fails, you need to make a load-form for the object, and
  you might legitimately expect a system to contain something to help you
  with that.  I found a function EXCL::MAKE-HASH-TABLE-LOAD-FORM in Allegro
  CL (for Unix) which appears to offer a reasonable efficiency gain:

CL-USER(37): (let ((table (make-hash-table :test #'equal)))
               (setf (gethash "foo" table) "bar")
               (setf (gethash "bar" table) "zot")
               (excl::make-hash-table-load-form table nil))
(let ((hash-table
       (make-hash-table
         :test 'equal
         :size 103
         :rehash-size 1.2
         :rehash-threshold 0.6407767)))
  (excl::hash-table-fill hash-table #("bar" "zot" "foo" "bar")))

  it appears that the other way to handle this, using the FASL read/write
  functions in Allegro CL (for Unix), actually produces the exact same
  function calls on loading as the above load-form.  this probably
  shouldn't come as a surprise, but, anyway, here's how to write and read a
  hash table to a file with the simplest FASL read/write mechanism:

CL-USER(47): (let ((table (make-hash-table :test #'equal)))
               (setf (gethash "foo" table) "bar")
               (setf (gethash "bar" table) "zot")
               (fasl-write table "/tmp/hash-table.fasl"))
t
CL-USER(48): (fasl-read "/tmp/hash-table.fasl")
(#<equal hash-table with 2 entries @ #x2076ae82>)

  if you wish to write more objects to the same file, use FASL-OPEN to get
  a FASL stream and write to it, instead:

CL-USER(53): (with-open-stream (fasl (fasl-open "/tmp/hash-table.fasl"))
               (let ((table (make-hash-table :test #'equal)))
                 (setf (gethash "foo" table) "bar")
                 (setf (gethash "bar" table) "zot")
                 (fasl-write table fasl)
                 (fasl-write table fasl)))
t
CL-USER(54): (fasl-read "/tmp/hash-table.fasl")
(#<equal hash-table with 2 entries @ #x20799e2a>
 #<equal hash-table with 2 entries @ #x2079a4d2>)

| i want to read the hash-table back from a file later, work with it,
| add/delete entries and then save it back.

  this sounds more like you actually want a "hash table" to reside on disk
  and have a mapping onto a simple file-based database.  or in other words,
  persistent objects.

  neither printable/loadable hash tables nor persistent objects are fully
  portable features in Common Lisp, i.e., you will need to use functions
  specific to the implementation in _some_ way.  other Common Lisps offer
  other ways than Allegro CL, but the above is at least how I do it.

#:Erik
--
  http://www.naggum.no/spam.html is about my spam protection scheme and how
  to guarantee that you reach me.  in brief: if you reply to a news article
  of mine, be sure to include an In-Reply-To or References header with the
  message-ID of that message in it.  otherwise, you need to read that page.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Bulent Murtezaoglu  
View profile  
 More options Jun 28 1998, 3:00 am
Newsgroups: comp.lang.lisp
From: Bulent Murtezaoglu <b...@acm.org>
Date: 1998/06/28
Subject: Re: writing/printing hash-tables to a file

* kp gores
| how do i "print" a hash-table to a file?
| i want to read the hash-table back from a file later, work
| add/delete entries and then save it back.

I think Erik's suggestion is good in that it illustrates how you can
work with a specific CL to do that you want.  The simple obvious
solution, of course, is to dump all the data items in the hash table
to a file individually and readably along with the count of items.
You can then read re-hash into a freshly created hash table of
suitable size the next time you start your lisp.  This is not very
efficient but trivial to implement and portable. It might just do what
you need if the file i/o only happens when you kill and re-start your
lisp session.

If the hash table is truly huge, you might still want to do some
implementation specific stuff to the garbage collector to minimize the
the scanning of what you know to be persistent data.  

BM


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »