Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Returning single or multiple values?

by sschneid (Deacon)
on Aug 27, 2004 at 19:10 UTC ( [id://386468]=perlquestion: print w/replies, xml ) Need Help??

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

I have the following code (a helper sub for Net::LDAP):
# Perform an LDAP query, returning results in a hash. sub fetch { my $self = shift; %{$self->{'ARG'}} = @_; my ($r); my $result = $self->{'LDAP'}->search( base => $self->{'ARG'}->{'source'}, filter => $self->{'ARG'}->{'filter'} ); foreach my $entry ($result->all_entries()) { map { push my @a, $entry->get_value($_); if ($a[1]) { $r->{$entry->dn()}->{$_} = [ $entry->get_value($_) ]; } else { $r->{$entry->dn()}->{$_} = $entry->get_value($_); } } $entry->attributes(); } return $r; }
...which works perfectly, and does exactly what I want. My only problem is that the assignment of values to a temporary array (@a) and then testing whether $a[1] exists as a way to tell whether $entry->get_value is returning multiple values or not seems... well, clumsy.

Is there a better way?

-s.

Replies are listed 'Best First'.
Re: Returning single or multiple values?
by ccn (Vicar) on Aug 27, 2004 at 19:27 UTC

    At first you do not check for existence of a second element of an array, but check it's truth.
    You can rewrite it like this:

    foreach my $entry ($result->all_entries()) { foreach ( $entry->attributes() ) { my $ra = [ $entry->get_value($_) ]; $r->{$entry->dn()}->{$_} = @$ra > 1 ? $ra : $ra->[0]; } }

    Update: lexical $r renamed to $ra

      Thanks!

      -s.
Re: Returning single or multiple values?
by Arunbear (Prior) on Aug 27, 2004 at 19:25 UTC
    Indeed there is:
    foreach my $entry ($result->all_entries()) { foreach ($entry->attributes()) { my (undef, $val) = $entry->get_value($_) $r->{$entry->dn()}{$_} = $val ? [ $entry->get_value($_) ] : $entry->get_value($_); } }
    Update: but see ccn's reply below for a better solution.
Re: Returning single or multiple values?
by bronto (Priest) on Aug 30, 2004 at 11:10 UTC

    Why are you needless calling get_value twice? It doesn't come for free! Moreover, why are you calling map that way? Personally, I don't like using map throwing away its result; if I need a cycle I prefer using an explicit foreach, for example.

    Anyway, my 2c:

    foreach my $entry ($result->all_entries()) { foreach my $attr ($entry->attributes()) { my @a = $entry->get_value($attr); $r->{$entry->dn()}->{$_} = @a > 1? \@a : $a[0] ; } }

    Ciao!
    --bronto


    The very nature of Perl to be like natural language--inconsistant and full of dwim and special cases--makes it impossible to know it all without simply memorizing the documentation (which is not complete or totally correct anyway).
    --John M. Dlugosz

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-04-23 23:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found