On Sep 14, 2012, at 12:13 AM, Chris Ridd wrote:
>
> On 13 Sep 2012, at 20:47, "Stierwalt, Kyle" <[email protected]> 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