From: Eric Abrahamsen <eric@ericabrahamsen.net>
To: help-gnu-emacs@gnu.org
Subject: Re: loading hash-table from a file?
Date: Thu, 05 Jan 2012 17:07:15 +0800 [thread overview]
Message-ID: <87r4zecybg.fsf@ericabrahamsen.net> (raw)
In-Reply-To: CAEjRLuSVMqrjXLX5nHwR08Hp3pmkTC-WsJnx5tj35bXAbQzMEQ@mail.gmail.com
On Thu, Jan 05 2012, ishi soichi wrote:
> I have never used hash-table of Emacs Lisp but I believe it's useful
> as a database.
>
> Say, I have a hundred key-value pairs, which are to be stored as
> hash-table.
> Can I save this as a (text?) file so that Emacs can load it when
> booting up?
>
> I am a little confused because if I use (cons key value), it outputs
> "(key . value)" which can be written in a regular file.
> If I wish to find the value from a key, all I need to do is to simply
> search through the file.
>
> Do you think I can save the database using hash-table like this?
>
> soichi
>
>
I found this on the internet (presumably authored by someone with the
initials T.V.), which does what you're asking (I used it to persist a
hash-table that I load on startup). I don't really know if it's superior
to the previous reply, but it certainly works. It produces a *.elc file,
which can be loaded with `load' (provided it's in your load-path, of
course). Whatever symbol name you originally dumped then becomes defined
again.
Eric
--8<---------------cut here---------------start------------->8---
(defun tv-dump-object-to-file (obj file)
"Save symbol object `obj' to the byte compiled version of `file'.
`obj' can be any lisp object, list, hash-table, etc...
`file' is an elisp file with ext *.el.
Loading the *.elc file will restitute object."
(require 'cl) ; Be sure we use the CL version of `eval-when-compile'.
(if (file-exists-p file)
(error "File already exists.")
(with-temp-file file
(erase-buffer)
(let* ((str-obj (symbol-name obj))
(fmt-obj (format "(setq %s (eval-when-compile %s))" str-obj str-obj)))
(insert fmt-obj)))
(byte-compile-file file) (delete-file file)
(message "`%s' dumped to %sc" obj file)))
--8<---------------cut here---------------end--------------->8---
--
GNU Emacs 24.0.92.2 (i686-pc-linux-gnu, GTK+ Version 2.24.8)
of 2012-01-04 on pellet
next prev parent reply other threads:[~2012-01-05 9:07 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-01-05 5:16 loading hash-table from a file? ishi soichi
2012-01-05 8:35 ` Valentin Baciu
2012-01-05 9:11 ` Teemu Likonen
2012-01-05 9:13 ` ishi soichi
2012-01-05 9:07 ` Eric Abrahamsen [this message]
2012-01-06 16:43 ` Thierry Volpiatto
2012-01-07 2:16 ` Eric Abrahamsen
2012-01-05 9:24 ` Teemu Likonen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=87r4zecybg.fsf@ericabrahamsen.net \
--to=eric@ericabrahamsen.net \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.