Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Populating hash keys and LDAP: putting things together

by dda (Friar)
on Oct 07, 2003 at 09:04 UTC ( [id://297234]=perlquestion: print w/replies, xml ) Need Help??

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

Recently I've asked two questions: Traverse LDAP Tree and Populating hash keys. The first one wasn't actually answered, and I decided to apply second way. It's too bad for me, but I can't understand how to implement the following task using techniques described in Populating hash keys.

What I have is a number of arrays containing LDAP dn`s split by ',' and reversed, as well as corresponding entries:

qw(p=root cp=1) qw(p=root cp=1 n=1) qw(p=root cp=1 n=1 n=1) qw(p=root cp=1 n=1 n=1 n=1) qw(p=root cp=1 n=1 n=1 n=1 n=1) qw(p=root cp=1 n=1 n=1 n=1 n=2) qw(p=root cp=1 n=1 n=1 n=1 n=3) qw(p=root cp=1 n=1 n=1 n=2) qw(p=root cp=1 n=1 n=1 n=2 n=1) qw(p=root cp=1 n=1 n=1 n=2 n=2) qw(p=root cp=1 n=1 n=1 n=2 n=3)
Each What I need is to create the following hash:
$href->{p=root}{cp=1} = 'entry1' $href->{p=root}{cp=1}{n=1) = 'entry11' #.. etc
Then I will be able to recursively walk trough it.

My code:

use strict; use Net::LDAP; use Text::ParseWords; use Data::Dumper; my $host = 'somehost'; my $port = 400; my $user = 'p=xxx'; my $pass = 'secret'; my $base = 'cp=1, p=root'; my $ldap = Net::LDAP->new($host, port => $port) or die "$@\n"; $ldap->bind($user, password => $pass); my $mesg = $ldap->search ( base => "$base", filter => "(objectClass=*)", ); $mesg->code && die $mesg->error . "\n"; my %entries; foreach my $entry ($mesg->all_entries) { my $dn = $entry->dn; $dn =~ s/\s+//g; $entry->dn($dn); my @path = reverse parse_line(",", 1, $dn); $entries{join ',', @path} = $entry; # this is not what I want exa +ctly } $ldap->unbind; foreach my $dn (sort keys %entries) { print "$dn\n"; } exit;

--dda

Replies are listed 'Best First'.
Re: Populating hash keys and LDAP: putting things together
by bart (Canon) on Oct 07, 2003 at 10:46 UTC
    What I need is to create the following hash:
    $href->{p=root}{cp=1} = 'entry1' $href->{p=root}{cp=1}{n=1} = 'entry11'
    Uh-uh. You can't have both. You see, if your create the latter first, then $href->{'p=root'{'cp=1'} will be a hash ref. And if you'd try to create them in the order above, you'll end up using 'entry1' as a symbolic reference for a hash %entry1, thus setting $entry1{'n=1'} = 'entry11';.

    With the data you've given, the only thing you can populate, is the following structure — even though I don't understand the structure of your values:

    $href->{'p=root'}{'cp=1'}{'n=1'}{'n=1'}{'n=1'}{'n=1'} = 'entry1111'; $href->{'p=root'}{'cp=1'}{'n=1'}{'n=1'}{'n=1'}{'n=2'} = 'entry1112'; $href->{'p=root'}{'cp=1'}{'n=1'}{'n=1'}{'n=1'}{'n=3'} = 'entry1113'; $href->{'p=root'}{'cp=1'}{'n=1'}{'n=1'}{'n=2'}{'n=1'} = 'entry1121'; $href->{'p=root'}{'cp=1'}{'n=1'}{'n=1'}{'n=2'}{'n=2'} = 'entry1122'; $href->{'p=root'}{'cp=1'}{'n=1'}{'n=1'}{'n=2'}{'n=3'} = 'entry1123'; # Let's see what we got: use Data::Dumper; $Data::Dumper::Indent = 1; print Dumper $href;
    Resulting in:
    $VAR1 = { 'p=root' => { 'cp=1' => { 'n=1' => { 'n=1' => { 'n=1' => { 'n=1' => 'entry1111', 'n=2' => 'entry1112', 'n=3' => 'entry1113' }, 'n=2' => { 'n=1' => 'entry1121', 'n=2' => 'entry1122', 'n=3' => 'entry1123' } } } } } };
    That's it. You can't put anything more in it. Not like his. You might have to find another data structure type to hold your data. Or you might have to reduce the amount of data you want to hold.
      Thanks. Now it is much more clear than before. I need to store a kind hierarchical menu, and values should be assigned to all nodes, not only to leaf nodes. That way, I need not only
      $href->{'p=root'}{'cp=1'}{'n=1'}{'n=1'}{'n=1'}{'n=1'} = 'entry1111';
      but also
      $href->{'p=root'}{'cp=1'} = 'entry1'; $href->{'p=root'}{'cp=1'}{'n=1'} = 'entry11'; $href->{'p=root'}{'cp=1'}{'n=1'}{'n=1'} ='entry111';
      What data structore can be used for this?

      --dda

        Well... one thing you could do, is when your data has a specific format — and it looks like it does, judging by your example, that everything contains an "=" sign — is add a special hash key for your normal values. I'd propose a '$', reminescent of the string suffix in BASIC, or the scalar sigil in Perl if you like.
        $href->{'p=root'}{'cp=1'}{'$'} = 'entry1'; $href->{'p=root'}{'cp=1'}{'n=1'}{'$'} = 'entry11'; $href->{'p=root'}{'cp=1'}{'n=1'}{'n=1'}{'$'} ='entry111'; $href->{'p=root'}{'cp=1'}{'n=1'}{'n=1'}{'n=1'}{'$'} ='entry1111';
        That should work well, without a conflict.

        If you need more kinds of (meta-)data, you can add more special keys.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://297234]
Approved by grinder
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-04-25 22:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found