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

Hey all, I am just trying to figure out how format the results for an LDAP search query using Net::LDAP. I am searching for users by name, and when I get more than one result I would like to display them all (present them to the user) in the following format:

USER #1:
AccountName,
FirstName,
LastName,
AccountNumber.
USER #2:
AccountName,
FirstName,
LastName,
AccountNumber.

After the user is presented with the following formatted results, they can then select #1 or #2, and a line will be built like follows:
AccountName,FirstName,LastName,AccountNumber
I have the following code thus far, but it only works for one result, and disregards any other results...
$mesg = $ldap->search( # perform a search base => "OU=BLA,DC=yum,DC=bum,DC=sum,DC=tum", filter => "(&(objectClass=user)(objectCategory= +person)(AccountType=805306368)( firstName=Dan*)(lastName=Jones))", attrs => [ 'firstName', 'lastName', 'accountNam +e', 'accountNumber' ] ); $mesg->code && die $mesg->error; foreach $entry ($mesg->entries) { $firstName = $entry->get_value("firstName"); $lastName = $entry->get_value("lastName"); $accountName = $entry->get_value("accountName"); $accountNumber = $entry->get_value("accountNumber"); } print "$firstName $lastName,$accountName,$accountNumber\n";
Now, this is perfect if there is one user, and if there is only one user this is how I want it, but trying to figure out how to display it for multiple results, and allow user to select one result out of multiple.
Any clarification, please ask! Thank you all.

Replies are listed 'Best First'.
Re: perl-ldap formatting question
by sflitman (Hermit) on Jan 08, 2009 at 05:07 UTC
    What's happening is that even if $mesg->entries returns multiple entries, you overwrite $firstName etc. each time through the loop, so your single print is the last entry received. Instead, store the info in a handy hash and then you can print a nice sorted list (or whatever):
    my ($acct,%records); foreach $entry ($mesg->entries) { $firstName = $entry->get_value("firstName"); $lastName = $entry->get_value("lastName"); $accountName = $entry->get_value("accountName"); $accountNumber = $entry->get_value("accountNumber"); $records{$accountName} = [ $firstName, $lastName, $accountNumber ]; } foreach $acct (sort keys %records) { print $acct,": ",join(' ',@{$records{$acct}}),"\n"; }

    HTH, SSF