* loading hash-table from a file?
@ 2012-01-05 5:16 ishi soichi
2012-01-05 8:35 ` Valentin Baciu
` (2 more replies)
0 siblings, 3 replies; 8+ messages in thread
From: ishi soichi @ 2012-01-05 5:16 UTC (permalink / raw
To: help-gnu-emacs
[-- Attachment #1: Type: text/plain, Size: 534 bytes --]
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
[-- Attachment #2: Type: text/html, Size: 670 bytes --]
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: loading hash-table from a file?
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:07 ` Eric Abrahamsen
2012-01-05 9:24 ` Teemu Likonen
2 siblings, 1 reply; 8+ messages in thread
From: Valentin Baciu @ 2012-01-05 8:35 UTC (permalink / raw
To: ishi soichi; +Cc: help-gnu-emacs
On Thu, Jan 5, 2012 at 7:16 AM, ishi soichi <soichi777@gmail.com> 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
In my Emacs 24, I can read back a printed (simple) hash table. I am
not sure weather using more complex data types as values will also
work.
;; Writing the hash table to a "database" file
(with-current-buffer (find-file-noselect "/tmp/x")
(let ((h (make-hash-table)))
(puthash 'a 0 h)
(puthash 'b 1 h)
(insert (format "%s" h))
(save-buffer)))
;; Reading back the hash-table object
(type-of (read (find-file-noselect "/tmp/x")))
;; hash-table
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: loading hash-table from a file?
2012-01-05 5:16 loading hash-table from a file? ishi soichi
2012-01-05 8:35 ` Valentin Baciu
@ 2012-01-05 9:07 ` Eric Abrahamsen
2012-01-06 16:43 ` Thierry Volpiatto
2012-01-05 9:24 ` Teemu Likonen
2 siblings, 1 reply; 8+ messages in thread
From: Eric Abrahamsen @ 2012-01-05 9:07 UTC (permalink / raw
To: help-gnu-emacs
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
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: loading hash-table from a file?
2012-01-05 8:35 ` Valentin Baciu
@ 2012-01-05 9:11 ` Teemu Likonen
2012-01-05 9:13 ` ishi soichi
0 siblings, 1 reply; 8+ messages in thread
From: Teemu Likonen @ 2012-01-05 9:11 UTC (permalink / raw
To: Valentin Baciu; +Cc: help-gnu-emacs
* 2012-01-05T10:35:18+02:00 * Valentin Baciu wrote:
> In my Emacs 24, I can read back a printed (simple) hash table. I am
> not sure weather using more complex data types as values will also
> work.
>
> ;; Writing the hash table to a "database" file
>
> (with-current-buffer (find-file-noselect "/tmp/x")
> (let ((h (make-hash-table)))
> (puthash 'a 0 h)
> (puthash 'b 1 h)
> (insert (format "%s" h))
> (save-buffer)))
Use the %S format character to print objects in machine-readable (i.e.,
READ) format.
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: loading hash-table from a file?
2012-01-05 9:11 ` Teemu Likonen
@ 2012-01-05 9:13 ` ishi soichi
0 siblings, 0 replies; 8+ messages in thread
From: ishi soichi @ 2012-01-05 9:13 UTC (permalink / raw
To: Teemu Likonen; +Cc: help-gnu-emacs
[-- Attachment #1: Type: text/plain, Size: 649 bytes --]
Thanks everyone. I think I works well!
soichi
2012/1/5 Teemu Likonen <tlikonen@iki.fi>
> * 2012-01-05T10:35:18+02:00 * Valentin Baciu wrote:
>
> > In my Emacs 24, I can read back a printed (simple) hash table. I am
> > not sure weather using more complex data types as values will also
> > work.
> >
> > ;; Writing the hash table to a "database" file
> >
> > (with-current-buffer (find-file-noselect "/tmp/x")
> > (let ((h (make-hash-table)))
> > (puthash 'a 0 h)
> > (puthash 'b 1 h)
> > (insert (format "%s" h))
> > (save-buffer)))
>
> Use the %S format character to print objects in machine-readable (i.e.,
> READ) format.
>
[-- Attachment #2: Type: text/html, Size: 1048 bytes --]
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: loading hash-table from a file?
2012-01-05 5:16 loading hash-table from a file? ishi soichi
2012-01-05 8:35 ` Valentin Baciu
2012-01-05 9:07 ` Eric Abrahamsen
@ 2012-01-05 9:24 ` Teemu Likonen
2 siblings, 0 replies; 8+ messages in thread
From: Teemu Likonen @ 2012-01-05 9:24 UTC (permalink / raw
To: ishi soichi; +Cc: help-gnu-emacs
* 2012-01-05T14:16:41+09:00 * ishi soichi wrote:
> 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?
Yes. Use the Lisp object printing functions to print to a buffer: PRINT,
PRIN1 or (FORMAT "%S" ...). Then read the printed object with READ which
turns it to a Lisp object again.
Here's an example of the idea:
;; Print
(with-temp-file "/tmp/test"
(let ((ht (make-hash-table)))
(puthash 'a 1 ht)
(puthash 'b 2 ht)
(print ht (current-buffer))))
;; Read
(with-current-buffer
(find-file-noselect "/tmp/test")
(let ((ht (read (current-buffer))))
(list (gethash 'a ht)
(gethash 'b ht))))
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: loading hash-table from a file?
2012-01-05 9:07 ` Eric Abrahamsen
@ 2012-01-06 16:43 ` Thierry Volpiatto
2012-01-07 2:16 ` Eric Abrahamsen
0 siblings, 1 reply; 8+ messages in thread
From: Thierry Volpiatto @ 2012-01-06 16:43 UTC (permalink / raw
To: help-gnu-emacs
Eric Abrahamsen <eric@ericabrahamsen.net> 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
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: loading hash-table from a file?
2012-01-06 16:43 ` Thierry Volpiatto
@ 2012-01-07 2:16 ` Eric Abrahamsen
0 siblings, 0 replies; 8+ messages in thread
From: Eric Abrahamsen @ 2012-01-07 2:16 UTC (permalink / raw
To: help-gnu-emacs
On Sat, Jan 07 2012, Thierry Volpiatto wrote:
> Eric Abrahamsen <eric@ericabrahamsen.net> writes:
>
[...]
>>
>> 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 ;-)
Hats off to you sir! I use it (or rather, its dumped files) every day.
--
GNU Emacs 24.0.92.2 (i686-pc-linux-gnu, GTK+ Version 2.24.8)
of 2012-01-04 on pellet
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2012-01-07 2:16 UTC | newest]
Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
2012-01-06 16:43 ` Thierry Volpiatto
2012-01-07 2:16 ` Eric Abrahamsen
2012-01-05 9:24 ` Teemu Likonen
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.