For Programmers: Free Programming Magazines  


Home > Archive > LDAP > October 2006 > Searching for Attributes with the same name









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Searching for Attributes with the same name
Y-Man

2006-10-13, 8:03 am

Hi

I'm attempting to search through a ldap entry and display all it's
attributes. Some of the attributes may have the same name but the code
that I'm using does only displays one of them.


$mesg = $ldap->search(filter=>"(uid=xyz)", base=>$base);
@entries = $mesg->entries;

foreach $entry (@entries) {

foreach $attr ($entry->attributes) {
print $attr." - ".$entry->get_value($attr)."\n";
}
}

Can anyone tell me what I'm doing wrong?

Thanks

Chris Ridd

2006-10-13, 7:04 pm

On 13/10/06 11:43, Y-Man <wcheung.groups@googlemail.com> wrote:

> Hi
>
> I'm attempting to search through a ldap entry and display all it's
> attributes. Some of the attributes may have the same name but the code
> that I'm using does only displays one of them.
>
>
> $mesg = $ldap->search(filter=>"(uid=xyz)", base=>$base);
> @entries = $mesg->entries;
>
> foreach $entry (@entries) {
>
> foreach $attr ($entry->attributes) {
> print $attr." - ".$entry->get_value($attr)."\n";
> }
> }
>
> Can anyone tell me what I'm doing wrong?
>


If called in a scalar context, get_value only returns one of the attribute's
values. Call it in a list context instead, so you have three nested loops:

foreach $entry (@entries) {
foreach $attr ($entry->attributes) {
foreach $val (@{$entry->get_value($attr)}) {
print "$attr - $val\n";
}
}
}

As I'm only testing in my mail client, I can't be sure if you absolutely
need the @{ and } around the call to get_value. But it forces list context,
so ought to work.

Cheers,

Chris


Chris Ridd

2006-10-13, 7:04 pm

On 13/10/06 6:57, Chris Ridd <chrisridd@mac.com> wrote:

> On 13/10/06 11:43, Y-Man <wcheung.groups@googlemail.com> wrote:
>
>
> If called in a scalar context, get_value only returns one of the attribute's
> values. Call it in a list context instead, so you have three nested loops:
>
> foreach $entry (@entries) {
> foreach $attr ($entry->attributes) {
> foreach $val (@{$entry->get_value($attr)}) {
> print "$attr - $val\n";
> }
> }
> }
>
> As I'm only testing in my mail client, I can't be sure if you absolutely
> need the @{ and } around the call to get_value. But it forces list context,
> so ought to work.


As Graham has just pointed out, that's wrong. I was possibly thinking of
array refs. The innermost loop should just be:

foreach $val ($entry->get_value($attr)) {
print "$attr - $val\n";
}

Cheers,

Chris


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com