Ask a Question related to PERL Beginners, Design and Development.
-
Tim Musson #1
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
-
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... -
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 = {... -
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 : ... -
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... -
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... -
Rob Hanson #2
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
-
Tim Musson #3
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



Reply With Quote

