Another reference question (hash of hash references)

Ask a Question related to PERL Beginners, Design and Development.

  1. #1

    Default Another reference question (hash of hash references)

    beginners,

    I am trying to build a hash of hash references. My problem is that I
    need to be able to add a key/value pair to the internal hashes...

    ,----- [ Here is my test code (yes, I am working with LDAP) ]
    #!perl
    use strict;
    use warnings;

    my %hash = (
    one => { uid => "uid1", cn => "cn1", group => "Group1" },
    two => { uid => "uid2", cn => "cn2", group => "Group2" },
    );
    $hash{"three"} = { uid => "uid3", cn => "cn3", group => "Group3" };

    $hash{"four"} = { uid => "uid4" };
    $hash{"four"} = { cn => "cn4" };
    $hash{"four"} = { group => "Group4" };

    my $key;
    foreach $key (sort keys %hash) {
    my $iHash = $hash{$key};
    print "
    key: $key
    hash-HRef: $hash{$key}
    HRef: $iHash
    hash/hash{uid}: $iHash->{uid}
    hash/hash{cn}: $iHash->{cn}
    hash/hash{group}: $iHash->{group}
    ";
    }
    `----- [ End of my test code ]

    So, I add the keys "one" and "two" when I initialize the hash, then I
    add "three" after. That works just fine, but I need to do something like
    "four", and all I get in the output is "Group4" because the third time I
    write the anon reference to the hash, it overwrites of course...

    Any suggestions on how I can do this? I don't think I can really use an
    array, and push to it because of how we plan to access the top level
    hash.

    Background, I am doing an LDAP query for a couple of attributes in one
    DN, then another LDAP query in a different part of the LDAP tree for
    some other attributes. The output for each query needs to be grouped by
    something like UID (I will replace "one", "two", etc with the UID/CN
    value as I extract it). Yes, it is a messed up LDAP structure, but I
    can't do anything about that... :-(

    Thanks for any help!

    --
    Tim Musson
    Flying with The Bat! eMail v1.62q
    Windows 2000 5.0.2195 (Service Pack 3)
    Contrary to popular belief, Unix is user friendly. It just happens to be
    selective about who it makes friends with

    Tim Musson Guest

  2. Similar Questions and Discussions

    1. Error-flag references to non-existing hash elements?
      I need a module that will support this behavior: use strict; my %hash = ('A' => 1, 'B' => 2); eval { my $x = $hash{C}; # reference to...
    2. Sort a hash based on values in the hash stored as arrays of hashes
      Hmm. I'm not quite sure if I got the subject right, but I'll try to explain. :-) I've got a hash of elements stored like this: $VAR1 = {...
    3. scalar reference in hash
      -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi all, I have a small question about using a scalar reference contained in a hash : ...
    4. hash reference to object methods
      >>>>> "BJ" == Ben Jones <bulk@thatproject.co.uk> writes: BJ> Hi, I'm farily new to oo perl, and wondered if either someone BJ> could help me with...
    5. sort array of hash references
      Hello, I am trying to sort an array of hash references, like what is returned by "statement_handle->fetchall_arrayref({})" when using DBI. I...
  3. #2

    Default RE: Another reference question (hash of hash references)

    > Any suggestions on how I can do this?

    Yes.

    # init the hash key
    $hash{four} = {} unless exists $hash{four};

    # store access the internal hash by following the reference "->"
    $hash{four}->{uid} = "uid4";
    $hash{four}->{cn} = "cn4";
    $hash{four}->{group} = "Group4";


    Rob


    -----Original Message-----
    From: Tim Musson [mailto:Tim@Musson.net]
    Sent: Friday, August 08, 2003 8:31 AM
    To: [email]beginners@perl.org[/email]
    Subject: Another reference question (hash of hash references)


    beginners,

    I am trying to build a hash of hash references. My problem is that I
    need to be able to add a key/value pair to the internal hashes...

    ,----- [ Here is my test code (yes, I am working with LDAP) ]
    #!perl
    use strict;
    use warnings;

    my %hash = (
    one => { uid => "uid1", cn => "cn1", group => "Group1" },
    two => { uid => "uid2", cn => "cn2", group => "Group2" },
    );
    $hash{"three"} = { uid => "uid3", cn => "cn3", group => "Group3" };

    $hash{"four"} = { uid => "uid4" };
    $hash{"four"} = { cn => "cn4" };
    $hash{"four"} = { group => "Group4" };

    my $key;
    foreach $key (sort keys %hash) {
    my $iHash = $hash{$key};
    print "
    key: $key
    hash-HRef: $hash{$key}
    HRef: $iHash
    hash/hash{uid}: $iHash->{uid}
    hash/hash{cn}: $iHash->{cn}
    hash/hash{group}: $iHash->{group}
    ";
    }
    `----- [ End of my test code ]

    So, I add the keys "one" and "two" when I initialize the hash, then I
    add "three" after. That works just fine, but I need to do something like
    "four", and all I get in the output is "Group4" because the third time I
    write the anon reference to the hash, it overwrites of course...

    Any suggestions on how I can do this? I don't think I can really use an
    array, and push to it because of how we plan to access the top level
    hash.

    Background, I am doing an LDAP query for a couple of attributes in one
    DN, then another LDAP query in a different part of the LDAP tree for
    some other attributes. The output for each query needs to be grouped by
    something like UID (I will replace "one", "two", etc with the UID/CN
    value as I extract it). Yes, it is a messed up LDAP structure, but I
    can't do anything about that... :-(

    Thanks for any help!

    --
    Tim Musson
    Flying with The Bat! eMail v1.62q
    Windows 2000 5.0.2195 (Service Pack 3)
    Contrary to popular belief, Unix is user friendly. It just happens to be
    selective about who it makes friends with


    --
    To unsubscribe, e-mail: [email]beginners-unsubscribe@perl.org[/email]
    For additional commands, e-mail: [email]beginners-help@perl.org[/email]
    Rob Hanson Guest

  4. #3

    Default Re: Another reference question (hash of hash references)

    Thanks Rob,

    My MUA believes you used Internet Mail Service (5.5.2653.19)
    to write the following on Friday, August 8, 2003 at 8:43:43 AM.

    HR> # store access the internal hash by following the reference "->"
    $hash{four}->>{uid} = "uid4";
    $hash{four}->>{cn} = "cn4";
    $hash{four}->>{group} = "Group4";

    I knew there would be an easy way, don't know why I didn't try that!

    Thanks again!

    --
    Tim Musson
    Flying with The Bat! eMail v1.62q
    Windows 2000 5.0.2195 (Service Pack 3)
    As I said before, I never repeat myself.

    Tim Musson Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139