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

I would like to generate a hash of hashes from ldap data.
my %hoh ; my $rec = { DN => $dn, MAIL => $mail, MEMBERS => [ @members ], PROXYADDRS => [ @email_addrs ], }; $hoh{$dn} = $rec;
But the hoh is not getting filled, if I print it with Data::Dumper I get an empty $VAR1. I must be doing something very stupid, but I do not see exactly what. Ok, let's show a bit more code :-).

Like I said, I query a ldap server with the Net::LDAP module. This works perfectly

my %hoh ; while ( my $entry = $search_ad->pop_entry() ) { my $displayname = $entry->get_value('cn'); my $dn = $entry->get_value('distinguishedname'); my $mail = $entry->get_value('mail'); my @members = $entry->get_value('member'); my @email_addrs = $entry->get_value('proxyaddresses'); # fill @ad_enabled push @ad_mail_enbld_groups, lc $mail; my $rec = { MAIL => $mail, MEMBERS => [ @members ], PROXYADDRS => [ @email_addrs ], }; $hoh{$dn} = $rec; }
If instead of a %hoh I push the $rec to an array referecence, it works. But I would rather have a hash of hashes to simplify a bit the code.

If I Dumper $rec, I can see the hash perfectly with the right values coming from the ldap server ..., grrr, tomorrow another go, time for bed.

ok, solved it. Indeed, operator error :( I was printing the wrong (empty) variable. Sorry for the noise.

Replies are listed 'Best First'.
Re: generate array of hashes
by jeffa (Bishop) on Dec 10, 2015 at 21:52 UTC

    You are doing something that we cannot see, the following works just fine:

    use strict; use warnings; my %hoh ; my ($dn,$mail) = qw( dn mail ); my @members = qw(foo bar baz); my @email_addrs = qw( foo@here bar@there ); my $rec = { DN => $dn, MAIL => $mail, MEMBERS => [ @members ], PROXYADDRS => [ @email_addrs ], }; $hoh{$dn} = $rec; use Data::Dumper; print Dumper \%hoh;

    While it is great to isolate your problem, be sure and post a complete example. It helps with the rubber ducking process. :)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      For completeness... GrandFather pointed this out to me not too long ago (I'm pretty sure of that... If I'm mistaken, I apologize). Rubber duck debugging. We all do it :)
Re: generate array of hashes
by GotToBTru (Prior) on Dec 10, 2015 at 21:59 UTC

    I think you've omitted the details that would help us help you.

    use Data::Dumper; $mail='foo@bar.com'; $dn = 'blah'; @members=('bob','carol','ted','alice'); $rec = { DN=>$dn, MAIL=>$mail, MEMBERS=>[@members] }; $hoh{$dn} = $rec; print Dumper($rec); print Dumper(\%hoh);
    $: perl 1149939.pl $VAR1 = { 'MAIL' => 'foo@bar.com', 'DN' => 'blah', 'MEMBERS' => [ 'bob', 'carol', 'ted', 'alice' ] }; $VAR1 = { 'blah' => { 'MAIL' => 'foo@bar.com', 'DN' => 'blah', 'MEMBERS' => [ 'bob', 'carol', 'ted', 'alice' ] } }; $:
    Dum Spiro Spero