Hash questions

Discussion in 'Perl Misc' started by lroland@gmail.com, Aug 4, 2005.

  1. Guest

    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).
     
    , Aug 4, 2005
    #1
    1. Advertising

  2. Guest

    "" <> 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
     
    , Aug 4, 2005
    #2
    1. Advertising

  3. 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
     
    Mark Clements, Aug 4, 2005
    #3
  4. Ala Qumsieh Guest

    wrote:
    > "" <> 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
     
    Ala Qumsieh, Aug 4, 2005
    #4
  5. David Wall Guest

    "" <> 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.
     
    David Wall, Aug 5, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Red Orchid
    Replies:
    3
    Views:
    1,063
  2. Pieter Claassen
    Replies:
    1
    Views:
    1,128
    CBFalconer
    Aug 4, 2004
  3. Bo Peng
    Replies:
    4
    Views:
    802
  4. rp
    Replies:
    1
    Views:
    556
    red floyd
    Nov 10, 2011
  5. Srijayanth Sridhar
    Replies:
    19
    Views:
    640
    David A. Black
    Jul 2, 2008
Loading...

Share This Page