Creating a hash of hashes

Discussion of Common Lisp
Post Reply
mcc
Posts: 18
Joined: Fri Mar 27, 2015 10:47 pm

Creating a hash of hashes

Post by mcc » Fri Apr 17, 2015 11:30 pm

Hi,

I am confused.
But this seems to be the very first step before being enlightened by the TAO of Lisp... :)

What I want to do is:
As a parameter to the function I get a list of items, which will become the keys of a "master-hash".
The values of each of the keys will become empty hashes, which I will fill afterwards.

My confusion starts when I think of the way of "ripping of the name of a hash, from the data object as such".
For me it seems that I need kinda "lambda()", but not for functions but for hashes. And if this sounds too
silly...its simply because I am trying to understand this magic language.

For example:

Code: Select all

(create-hash master-hash :size 10)
creates a hash for me with 10 empty slots -- the master hash.

Code: Select all

(create-hash sub-hash)
This will become one of the sub-hashes. But it already assigns the data object to the symbol/name/variable "sub-hash".

Next step would be something like (WARNING! WRONG CODE AHEAD!)

Code: Select all

(setf (gethash ' masterhash) sub-hash sub-hash )
No...this looks terrible wrong (even for my unknowing eyes ;) )

I googled the Web and looked into Pratcical Lisp
(http://www.gigamonkeys.com/book/collections.html)
but only found examples, which handles one-dimensional hashes.

How can I create a string from a symbol (how can I get its "name"...) ?
How can I get this working (and myself understand, how its works and why)?

Thank you very much in advance for any help!
Have a nice weekend! :)
Best regards,
mcc

edgar-rft
Posts: 226
Joined: Fri Aug 06, 2010 6:34 am
Location: Germany

Re: Creating a hash of hashes

Post by edgar-rft » Sat Apr 18, 2015 9:30 am

Setting the value of a sub-hashtable:

Code: Select all

(setf (gethash sub-key (gethash master-key master-table)) sub-value)
Using multi-dimensional hashtables leads to code that nobody understands.
mcc wrote:How can I create a string from a symbol (how can I get its "name"...) ?
By using SYMBOL-NAME ...

- edgar

mcc
Posts: 18
Joined: Fri Mar 27, 2015 10:47 pm

Re: Creating a hash of hashes

Post by mcc » Sat Apr 18, 2015 6:52 pm

Hi Edgar,

thanks for your reply and help! :)

Best regards,
mcc

hajovonta
Posts: 17
Joined: Wed Aug 24, 2011 12:42 am

Re: Creating a hash of hashes

Post by hajovonta » Thu Jul 02, 2015 6:40 am

edgar-rft wrote:Setting the value of a sub-hashtable:

Code: Select all

(setf (gethash sub-key (gethash master-key master-table)) sub-value)
Using multi-dimensional hashtables leads to code that nobody understands.
mcc wrote:How can I create a string from a symbol (how can I get its "name"...) ?
By using SYMBOL-NAME ...

- edgar
Edgar,

apart from this, are there any performance advantages?

Goheeca
Posts: 271
Joined: Thu May 10, 2012 12:54 pm
Contact:

Re: Creating a hash of hashes

Post by Goheeca » Thu Jul 02, 2015 8:38 am

There is another possibility: using a single hash-table and a multidimensional key. Having string keys, the default eql test is probably insufficient, you'd like to upgrade test function to equal and then you can reasonably use cons pairs as key as well. Or write own test function tailored to your keys and speed requests.
cl-2dsyntax is my attempt to create a Python-like reader. My mirror of CLHS (and the dark themed version). Temporary mirrors of aferomentioned: CLHS and a dark version.

GengyangCai
Posts: 14
Joined: Sun Jul 08, 2012 3:50 pm

Re: Creating a hash of hashes

Post by GengyangCai » Fri Jul 03, 2015 8:03 am

Strangely enough, when I ran all of the code examples given in this topic, none of them worked at all. I have copy and pasted my code below ... if anyone can give any enlightenment on this issue, that would be great !

CL-USER 1 > (create-hash master-hash :size 10)

Error: Undefined operator CREATE-HASH in form (CREATE-HASH MASTER-HASH :SIZE 10).
1 (continue) Try invoking CREATE-HASH again.
2 Return some values from the form (CREATE-HASH MASTER-HASH :SIZE 10).
3 Try invoking something other than CREATE-HASH with the same arguments.
4 Set the symbol-function of CREATE-HASH to another function.
5 Set the macro-function of CREATE-HASH to another function.
6 (abort) Return to level 0.
7 Return to top loop level 0.

Type :b for backtrace or :c <option number> to proceed.
Type :bug-form "<subject>" for a bug report template or :? for other options.

CL-USER 2 : 1 > (create-hash master-hash :size 10)

Error: Undefined operator CREATE-HASH in form (CREATE-HASH MASTER-HASH :SIZE 10).
1 (continue) Try invoking CREATE-HASH again.
2 Return some values from the form (CREATE-HASH MASTER-HASH :SIZE 10).
3 Try invoking something other than CREATE-HASH with the same arguments.
4 Set the symbol-function of CREATE-HASH to another function.
5 Set the macro-function of CREATE-HASH to another function.
6 (abort) Return to level 1.
7 Return to debug level 1.
8 Return to level 0.
9 Return to top loop level 0.

Type :b for backtrace or :c <option number> to proceed.
Type :bug-form "<subject>" for a bug report template or :? for other options.

CL-USER 3 : 2 > (create-hash sub hash)

Error: Undefined operator CREATE-HASH in form (CREATE-HASH SUB HASH).
1 (continue) Try invoking CREATE-HASH again.
2 Return some values from the form (CREATE-HASH SUB HASH).
3 Try invoking something other than CREATE-HASH with the same arguments.
4 Set the symbol-function of CREATE-HASH to another function.
5 Set the macro-function of CREATE-HASH to another function.
6 (abort) Return to level 2.
7 Return to debug level 2.
8 Return to level 1.
9 Return to debug level 1.
10 Return to level 0.
11 Return to top loop level 0.

Type :b for backtrace or :c <option number> to proceed.
Type :bug-form "<subject>" for a bug report template or :? for other options.

CL-USER 4 : 3 > (setf (gethash sub-key (gethash master-key master-table)) sub-value)

Error: The variable SUB-KEY is unbound.
1 (continue) Try evaluating SUB-KEY again.
2 Specify a value to use this time instead of evaluating SUB-KEY.
3 Specify a value to set SUB-KEY to.
4 (abort) Return to level 3.
5 Return to debug level 3.
6 Return to level 2.
7 Return to debug level 2.
8 Return to level 1.
9 Return to debug level 1.
10 Return to level 0.
11 Return to top loop level 0.

Type :b for backtrace or :c <option number> to proceed.
Type :bug-form "<subject>" for a bug report template or :? for other options.

CL-USER 5 : 4 > (setf (gethash sub-key (gethash master-key master-table)) sub-value)

Error: The variable SUB-KEY is unbound.
1 (continue) Try evaluating SUB-KEY again.
2 Specify a value to use this time instead of evaluating SUB-KEY.
3 Specify a value to set SUB-KEY to.
4 (abort) Return to level 4.
5 Return to debug level 4.
6 Return to level 3.
7 Return to debug level 3.
8 Return to level 2.
9 Return to debug level 2.
10 Return to level 1.
11 Return to debug level 1.
12 Return to level 0.
13 Return to top loop level 0.

Type :b for backtrace or :c <option number> to proceed.
Type :bug-form "<subject>" for a bug report template or :? for other options.

edgar-rft
Posts: 226
Joined: Fri Aug 06, 2010 6:34 am
Location: Germany

Re: Creating a hash of hashes

Post by edgar-rft » Sat Jul 04, 2015 8:01 am

GengyangCai wrote:...all of the code examples given in this topic, none of them worked at all...
This was an additional question to another thread about converting a set of text files into a SQL database (that I currently can't find anymore), and if I remember right then CREATE-HASH was a function defined by mcc (the original poster), at least there is no CREATE-HASH function defined in Common Lisp. You did nothing wrong, it's just the definition of the CREATE-HASH function that's missing in this thread.

Also my code examples above contain a lot of undefined variables, whose names were meant to explain what the respective variables do, but it's in no way functioning code.

See the examples in the Common Lisp Hyperspec under MAKE-HASH-TABLE how to create a hashtable and GETHASH how to get and modify the values.

- edgar

Post Reply