Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Hash questions

Reply
Thread Tools

Hash questions

 
 
lroland@gmail.com
Guest
Posts: n/a
 
      08-04-2005
Hi all

I use a the OO capabilities of Perl to define a data structure using a
hash of hashes - this unfortunately implies that the resulting data
structure is modifiable (ideally only the values should be modifiable
not the structure of the hash (i.e. it should be impossible to add any
new keys)). So my questions goes:


1) Is it possible to define a data structure using a hash where only
the values of the keys is modifiable (i.e. much like a struct in C).
i.e I want to alter this function to somehow return a structure:

sub new
{
my ($this) = @_;
bless {
key1 => undef,
key2 => {
key2a => undef
}
}, $this;
}

where it it is not possible to do something like this:

my $test = new();
$test->{key2}->{key2b} = "24";

but this should be possible:

$test->{key2}->{key2a} = "42"

can this be done ?

2) Is there a generic way of traversing all the key/value pairs in a
hash of hashes (I want to create a function to check if all the values
in my "hash of hashes" are defined) - googling around seams to indicate
that using "keys" will only work on the outer level of a hash of hashes
which implies that you will need to nest calls to "keys" which will
require knowledge of the structure of the hash of hashes (recursive
calls could solve this however).

 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      08-04-2005
"(E-Mail Removed)" <(E-Mail Removed)> wrote:
> Hi all
>
> I use a the OO capabilities of Perl to define a data structure using a
> hash of hashes - this unfortunately implies that the resulting data
> structure is modifiable (ideally only the values should be modifiable
> not the structure of the hash (i.e. it should be impossible to add any
> new keys)). So my questions goes:
>
> 1) Is it possible to define a data structure using a hash where only
> the values of the keys is modifiable (i.e. much like a struct in C).


Yes, but if you are into programming bondage and discipline, Perl
probably shouldn't be your language choice in the first place. Anyway,
you could implement your objects as closures, or as inside-out objects.
This would prevent all access to the object that doesn't go through the
objects methods, so the methods would enforce the non-modification of the
structure.


> i.e I want to alter this function to somehow return a structure:
>
> sub new
> {
> my ($this) = @_;
> bless {
> key1 => undef,
> key2 => {
> key2a => undef
> }
> }, $this;
> }
>
> where it it is not possible to do something like this:
>
> my $test = new();
> $test->{key2}->{key2b} = "24";
>
> but this should be possible:
>
> $test->{key2}->{key2a} = "42"


Why should that be possible? It isn't very OO. You could probably
somehow use tie to tie the hash to an object that implements this, but
I don't know how circumventable that is.


> can this be done ?
>
> 2) Is there a generic way of traversing all the key/value pairs in a
> hash of hashes (I want to create a function to check if all the values
> in my "hash of hashes" are defined) - googling around seams to indicate
> that using "keys" will only work on the outer level of a hash of hashes
> which implies that you will need to nest calls to "keys" which will
> require knowledge of the structure of the hash of hashes (recursive
> calls could solve this however).


Yes, recursive calls are the generic way of doing that.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
Usenet Newsgroup Service $9.95/Month 30GB
 
Reply With Quote
 
 
 
 
Mark Clements
Guest
Posts: n/a
 
      08-04-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Hi all
>
> I use a the OO capabilities of Perl to define a data structure using a
> hash of hashes - this unfortunately implies that the resulting data
> structure is modifiable (ideally only the values should be modifiable
> not the structure of the hash (i.e. it should be impossible to add any
> new keys)). So my questions goes:
>
>
> 1) Is it possible to define a data structure using a hash where only
> the values of the keys is modifiable (i.e. much like a struct in C).
> i.e I want to alter this function to somehow return a structure:
>
> sub new
> {
> my ($this) = @_;
> bless {
> key1 => undef,
> key2 => {
> key2a => undef
> }
> }, $this;
> }
>
> where it it is not possible to do something like this:
>
> my $test = new();
> $test->{key2}->{key2b} = "24";
>
> but this should be possible:
>
> $test->{key2}->{key2a} = "42"
>
> can this be done ?


check out lock_keys in Hash::Util

Also, see

perldoc fields

Mark
 
Reply With Quote
 
Ala Qumsieh
Guest
Posts: n/a
 
      08-04-2005
(E-Mail Removed) wrote:
> "(E-Mail Removed)" <(E-Mail Removed)> wrote:
>>2) Is there a generic way of traversing all the key/value pairs in a
>>hash of hashes (I want to create a function to check if all the values
>>in my "hash of hashes" are defined) - googling around seams to indicate
>>that using "keys" will only work on the outer level of a hash of hashes
>>which implies that you will need to nest calls to "keys" which will
>>require knowledge of the structure of the hash of hashes (recursive
>>calls could solve this however).

>
>
> Yes, recursive calls are the generic way of doing that.


I believe the OP is looking for something like:

for my $key1 (%hash) {
for my $key2 (%{$hash{$key1}}) {
# etc ...
}
}

--Ala
 
Reply With Quote
 
David Wall
Guest
Posts: n/a
 
      08-05-2005
"(E-Mail Removed)" <(E-Mail Removed)> wrote:

> I use a the OO capabilities of Perl to define a data structure
> using a hash of hashes - this unfortunately implies that the
> resulting data structure is modifiable (ideally only the values
> should be modifiable not the structure of the hash (i.e. it
> should be impossible to add any new keys)). So my questions
> goes:
>
>
> 1) Is it possible to define a data structure using a hash where
> only the values of the keys is modifiable (i.e. much like a
> struct in C). i.e I want to alter this function to somehow
> return a structure:


If you don't already have a copy of Damian Conway's book _Object
Oriented Perl_, it's highly recommended by many folks in the Perl
community. See http://books.perl.org/book/171

It seems there's a David Wall whose review of the book is at the link
above. I certainly don't remember writing that review. My name
isn't a very rare one, though. I just didn't know about this
instance of it.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
hash of hash of hash of hash in c++ rp C++ 1 11-10-2011 04:45 PM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
In 'HashMap.put', "if (e.hash == hash && eq(k, e.key))" ? Red Orchid Java 3 01-30-2006 07:04 PM
standard library for hash table storage and hash algorithm Pieter Claassen C Programming 1 08-04-2004 03:11 AM
Re: Questions....questions....questions Patrick Michael A+ Certification 0 06-16-2004 04:53 PM



Advertisments