Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

storing data via 2 sibling keys

by AidanLee (Chaplain)
on Jun 19, 2001 at 00:33 UTC ( [id://89437]=perlquestion: print w/replies, xml ) Need Help??

AidanLee has asked for the wisdom of the Perl Monks concerning the following question:

I am working out a way to store the relationships between SQL tables for quick retrival. So if I have table A and table B, I'd like to know the relationship between the two. In a pseudo-xml (working towards evovling this to RDF) I'd represent relationships thusly:

<relationships> <direct key1="csUsers.ID" key2="csUsers_lang.ID" /> <indirect key1="csUsers.ID" key2="csGroups.ID" > <direct key1="csUsers.ID" key2="csUsers_Groups.ID" /> <direct key1="csGroups.ID" key2="csUsers_Groups.ID" /> </indirect> </relationships>

I'd parse this in and cache it using Data::Dumper so I'd only have to re-parse when the data changes. The question here though is i'd want to be able to ask "how does csUsers relate to csGroups?" AND also be able to ask "how does csUsers relate to csGroups?"

So what comes to mind is a hash with two keys, which of course doesn't exist. I could nest hashes, but then that gives an order to the keys $hash{csUsers}{csGroups} is different than $hash{csGroups}{csUsers}. I can come up with arbitrary (read: alphabetic) ways to nest the hashes, but I'd rather not. Any suggestions on how to simulate a hash that takes a pair of keys in arbitraty order to return a value?

Replies are listed 'Best First'.
Re: storing data via 2 sibling keys
by btrott (Parson) on Jun 19, 2001 at 00:56 UTC
    How about something like this:
    package Tie::HashNKeys; use strict; sub TIEHASH { my $class = shift; bless {}, $class; } sub STORE { $_[0]->{ join $;, sort split /$;/, $_[1] } = $_[2]; } sub FETCH { $_[0]->{ join $;, sort split /$;/, $_[1] }; }
    Sample:
    tie my %hash, 'Tie::HashNKeys'; $hash{'csUsers','csGroups'} = 'foo'; print $hash{'csGroups','csUsers'}; ## prints 'foo'
    Is that kind of what you want?
      And once more my horizon is widened. Thanks btrott, hadn't even considered ties. This will make for a nice transparent way of doing this. Does anyone know what Data::Dumper will think of it though?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://89437]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2024-04-20 01:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found