Beast am Mittwoch, 31. Mai 2006 12.06:
> Beast wrote:
> > Hi,
> >
> > I have some rather big chunk of data, returned from ldap server. The
> > format is simple:
> > "Displa name" <[EMAIL PROTECTED]>
> >
> > Simply displying data "as is" is simple, but how do I sorted by
> > "display name"?
> > pls note that "display name" contains space and might not unique so I
> > can't put it on %hash.
> > many thanks.
>
> Many thanks to all who helps, it worked now.
Hi again
fine it works :-)
Nevertheless, let me make some annotations to the code below:
> ----
# some code omitted in the original
> my $ldap = Net::LDAP->new($ldap_host) or die "$@";
> my $mesg = $ldap->bind(version => 3);
>
> $mesg = $ldap->search( base => $ldap_base,
> filter => "(&(objectclass=mailuser)($dsp_opt))",
> scope => "sub",
> attrs => ['displayname', 'mail'] );
>
> my $max = $mesg->count;
$max is not necessary, see below.
> my @unsorted = ();
> my @sorted = ();
my (@unsorted, @sorted); # shorter; @sorted is never used
> for (my $i=0; $i<$max; $i++) {
This is the C way. In perl you can express it shorter, you even don't need an
explicit counter variable $i:
for (0..$mesg->count) { # counter var is in $_
> my $entry = $mesg->entry($i);
my $entry = $mesg->entry($_);
> my $display_name = $entry->get_value('displayname');
> my $mail_address = $entry->get_value('mail');
> push @unsorted, [$display_name, $mail_address];
You could omit local variables (and populating them)
by replacing these 3 lines with:
push @unsorted, [$entry->get_value('displayname'), $entry->get_value('mail')];
> }
>
> foreach my $cn (sort { lc($a->[0]) cmp lc ($b->[0])} @unsorted) {
> print "\"$cn->[0]\" <$cn->[1]>\n";
> }
or, shorter:
print "\"$_->[0]\" <$_->[1]>\n"
for sort { lc($a->[0]) cmp lc ($b->[0])} @unsorted;
[everything untested]
Dani
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>