Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Search and print unique LDAP attribute values.

41 views
Skip to first unread message

Stierwalt, Kyle

unread,
Sep 13, 2012, 3:47:43 PM9/13/12
to perl...@perl.org
New to Perl, what I'm trying to do is print out the unique values for a given LDAP attribute.

1. Does the while portion of this look correct?
2. If so, how do I print out the values of the hash?


...snip...
my $iDirLdap = &Common::connectToiDir2("ldap.edu");

my $iDirResult = $iDirLdap->search (
base => "dc=ldap,dc=edu",
filter => "EduUserRole=*",
attrs => ['EduUserRole'],
);
while( my $entry = $iDirResult->pop_entry())
{
my @a = $entry->get_value('EduUserRole');
my @result = ();
my %seen = ();
foreach (@a)
{
next if ($seen{$_});
$seen{$_} = 1;
push (@result, $_);
}
}


What goes here to print out unique values?


&Common::disconnectFromiDir($iDirLdap);
exit;

Chris Ridd

unread,
Sep 14, 2012, 3:13:39 AM9/14/12
to Stierwalt, Kyle, perl...@perl.org

On 13 Sep 2012, at 20:47, "Stierwalt, Kyle" <KStie...@fsu.edu> wrote:

> New to Perl, what I'm trying to do is print out the unique values for a given LDAP attribute.

By definition the values in a given stored LDAP attribute have to be all unique, so most of your problem "goes away": @result is simply @a!

Cheers,

Chris

Bruce Johnson

unread,
Sep 14, 2012, 11:46:38 AM9/14/12
to perl...@perl.org

On Sep 14, 2012, at 12:13 AM, Chris Ridd wrote:

>
> On 13 Sep 2012, at 20:47, "Stierwalt, Kyle" <KStie...@fsu.edu> wrote:
>
>> New to Perl, what I'm trying to do is print out the unique values for a given LDAP attribute.

to directly snswer your question, if @results really does contain all the unique values the code is:


foreach (@result) {print $_;}


>
> By definition the values in a given stored LDAP attribute have to be all unique, so most of your problem "goes away": @result is simply @a!

I'm pretty sure that the method here will only work with single-valued attributes.

How I do it (our ldap server provides a large mix of single-value and multi-value attributes) is outlined in the pods for Net::LDAP:

http://search.cpan.org/~gbarr/perl-ldap-0.4001/lib/Net/LDAP/Examples.pod
http://search.cpan.org/~gbarr/perl-ldap-0.4001/lib/Net/LDAP/Search.pod

Look for using for using as_struct():

as_struct ( )
Returns a reference to a HASH, where the keys are the DNs of the results and the values are HASH references. These second level HASHes hold the attributes such that the keys are the attribute names, in lowercase, and the values are references to an ARRAY holding the values.

In practice my code looks like this (the script is generating an html page)

$mesg = $ldaps->search ( # perform a search
base => $searchBase,
filter => $searchFilter
);

$mesg->code && die $mesg->error;

my $searchhash = $mesg->as_struct;

my @returnedDNs = keys %$searchhash; #Each member of @returnedDNs is equivalent to $mesg->entry(N); one element for each returned record.

my ($dn,$valref, @attrnames, $attr,$attrval, $actval);

foreach $dn (@returnedDNs){
print "<tr><td colspan=2><b>$dn Data</b></td></tr>";
$valref = $$searchhash{$dn}; #Returns a pointer to the hash of attribute{value arrays} for this dn result.
@attrnames = sort keys %$valref; #this gets me an array of the value names, as the hash that $valref points to
foreach $attr (@attrnames){ #now cycle through each attribute
$attrval = @$valref{$attr};# get a pointer to the value array for each attribute
foreach $actval (@$attrval){# cycle through the array of values for each attribute
print "<tr><td><b>$attr</b></td><td>$actval</td></tr>"
}
}
}


--
Bruce Johnson
University of Arizona
College of Pharmacy
Information Technology Group

Institutions do not have opinions, merely customs


Chris Ridd

unread,
Sep 14, 2012, 12:38:01 PM9/14/12
to Bruce Johnson, perl...@perl.org

On 14 Sep 2012, at 16:46, Bruce Johnson <joh...@pharmacy.arizona.edu> wrote:

>
> On Sep 14, 2012, at 12:13 AM, Chris Ridd wrote:
>>
>> By definition the values in a given stored LDAP attribute have to be all unique, so most of your problem "goes away": @result is simply @a!
>
> I'm pretty sure that the method here will only work with single-valued attributes.

It'll work just fine with multi-valued attributes - the trick is that you have to call get_value in an array context, and then it'll return an array of all the values. Otherwise it'll just return one of them.

See <http://search.cpan.org/~gbarr/perl-ldap-0.4001/lib/Net/LDAP/Entry.pod#get_value>

I usually use it something like this:

my (@vals) = $entry->get_value("objectClass");
foreach my $val (@vals) {
...
}

Chris

Bruce Johnson

unread,
Sep 14, 2012, 12:41:50 PM9/14/12
to perl...@perl.org
cool, that's simpler than the as_struct method.
0 new messages