From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Eric Abrahamsen Newsgroups: gmane.emacs.help Subject: Re: loading hash-table from a file? Date: Thu, 05 Jan 2012 17:07:15 +0800 Message-ID: <87r4zecybg.fsf@ericabrahamsen.net> References: NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: dough.gmane.org 1325754462 9564 80.91.229.12 (5 Jan 2012 09:07:42 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Thu, 5 Jan 2012 09:07:42 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Jan 05 10:07:38 2012 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([140.186.70.17]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1RijIP-0006HA-H1 for geh-help-gnu-emacs@m.gmane.org; Thu, 05 Jan 2012 10:07:37 +0100 Original-Received: from localhost ([::1]:51990 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RijIO-0001jP-TL for geh-help-gnu-emacs@m.gmane.org; Thu, 05 Jan 2012 04:07:36 -0500 Original-Received: from eggs.gnu.org ([140.186.70.92]:38838) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RijIK-0001j9-QC for help-gnu-emacs@gnu.org; Thu, 05 Jan 2012 04:07:33 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RijIG-0003tC-EC for help-gnu-emacs@gnu.org; Thu, 05 Jan 2012 04:07:32 -0500 Original-Received: from lo.gmane.org ([80.91.229.12]:47731) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RijIG-0003t3-4H for help-gnu-emacs@gnu.org; Thu, 05 Jan 2012 04:07:28 -0500 Original-Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1RijID-0006CQ-ON for help-gnu-emacs@gnu.org; Thu, 05 Jan 2012 10:07:25 +0100 Original-Received: from 114.250.133.120 ([114.250.133.120]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 05 Jan 2012 10:07:25 +0100 Original-Received: from eric by 114.250.133.120 with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 05 Jan 2012 10:07:25 +0100 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 53 Original-X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: 114.250.133.120 User-Agent: Gnus/5.110018 (No Gnus v0.18) Emacs/24.0.92 (gnu/linux) Cancel-Lock: sha1:wbc9S0TAeiYpgCWElr/jI6dAnQU= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-Received-From: 80.91.229.12 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:83346 Archived-At: 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