From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Thierry Volpiatto Newsgroups: gmane.emacs.help Subject: Re: loading hash-table from a file? Date: Fri, 06 Jan 2012 17:43:41 +0100 Message-ID: <8762gou6gy.fsf@gmail.com> References: <87r4zecybg.fsf@ericabrahamsen.net> 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 1325868272 4206 80.91.229.12 (6 Jan 2012 16:44:32 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Fri, 6 Jan 2012 16:44:32 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Jan 06 17:44:28 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 1RjCu4-00028k-IQ for geh-help-gnu-emacs@m.gmane.org; Fri, 06 Jan 2012 17:44:28 +0100 Original-Received: from localhost ([::1]:60809 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RjCu4-0005d1-4Y for geh-help-gnu-emacs@m.gmane.org; Fri, 06 Jan 2012 11:44:28 -0500 Original-Received: from eggs.gnu.org ([140.186.70.92]:49983) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RjCtn-0005bD-4V for help-gnu-emacs@gnu.org; Fri, 06 Jan 2012 11:44:24 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RjCtb-0007xk-Gu for help-gnu-emacs@gnu.org; Fri, 06 Jan 2012 11:44:10 -0500 Original-Received: from lo.gmane.org ([80.91.229.12]:45408) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RjCta-0007vp-Rs for help-gnu-emacs@gnu.org; Fri, 06 Jan 2012 11:43:59 -0500 Original-Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1RjCtW-0001sB-Tt for help-gnu-emacs@gnu.org; Fri, 06 Jan 2012 17:43:54 +0100 Original-Received: from 114.78.88.79.rev.sfr.net ([79.88.78.114]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 06 Jan 2012 17:43:54 +0100 Original-Received: from thierry.volpiatto by 114.78.88.79.rev.sfr.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Fri, 06 Jan 2012 17:43:54 +0100 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 58 Original-X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: 114.78.88.79.rev.sfr.net User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.0.92 (gnu/linux) Cancel-Lock: sha1:epJV5RMT3fxYJIIBdJydWR7c8Pc= 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:83378 Archived-At: Eric Abrahamsen writes: > 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 > > (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))) I use this always to save emacs session (buffers and variables), but it have limitations on complex hash-tables, windows objects etc... T.V are my initials ;-) -- Thierry Get my Gnupg key: gpg --keyserver pgp.mit.edu --recv-keys 59F29997