Home > Archive > LDAP > January 2008 > Natural Sort Function?
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 |
Natural Sort Function?
|
|
| Patrick Cable 2008-01-21, 8:10 pm |
| Disclaimer: I'm new to perl (just started doing work with it a w
ago), so perhaps I'm either making this more complicated than it needs
to be, or I'm doing something that *is* complicated, and I don't know
where to start.
I wrote a script to query our Hosts OU. The output will be IP then
Hostname. The code I have right now to print the results is:
foreach $result ($mesg->sorted(ipHostNumber)) {
$cn = $result->get_value('cn');
$ip = $result->get_value('ipHostNumber');
write;
}
However, as you can imagine, the output I get is machine sorted -
cable@interceptor:~/views/ldap-utils$ ./hlookup -i x.y.z.*
IP HOST
x.y.z.109 hosta
x.y.z.11 hostb
x.y.z.110 hostc
x.y.z.111 hostd
Is there an easy way to make this do some sort of "natural sort?" I
tried substituting Sort::Naturally's 'ncmp' function in
Net/LDAP/Search.pm but it didnt do anything. I tried a few other things
involving hash functions (it seems that $mesg->entries returns a hash,
according to some print output I've experimented with), but no dice
there either.
If anyone could point me in the right direction on how to do what I'm
trying to do, I'd be very appreciative.
--
// Patrick Cable II
// LLCAD System Administrator Co-Op
// 781-981-5996 - cable@ll.mit.edu
| |
| Graham Barr 2008-01-21, 8:10 pm |
| On Mon, January 21, 2008 2:08 pm, Patrick Cable wrote:
> foreach $result ($mesg->sorted(ipHostNumber)) {
> $cn = $result->get_value('cn');
> $ip = $result->get_value('ipHostNumber');
> write;
> }
$mesg->sorted was written without thought for comparing anything other
than strings. But if you are using only a single field then you can do the
sort yourself with a Schwartzian transform
my @results = map { $_->[0] }
sort { ncmp($a->[1], $b->[1]) }
map { [ $_, $result->get_value('ipHostNumber') ] }
$mesg->entries;
foreach $result (@results) {
$cn = $result->get_value('cn');
$ip = $result->get_value('ipHostNumber');
write;
}
Graham.
| |
| Patrick Cable 2008-01-21, 8:10 pm |
| Graham Barr wrote:
> my @results = map { $_->[0] }
> sort { ncmp($a->[1], $b->[1]) }
> map { [ $_, $result->get_value('ipHostNumber') ] }
> $mesg->entries;
I get an "Can't call method 'get_value' on an undefined value" error
with this code, and it references the sort{} line. Should I define
$result before the array? If so, what should I define it as? I tried
changing the last line as "$result = mesg->entries" however I had no
dice there.
--
// Patrick Cable II
// LLCAD System Administrator Co-Op
// 781-981-5996 - cable@ll.mit.edu
| |
| Patrick Cable 2008-01-21, 8:10 pm |
| Completely by chance, I was able to solve this one on my own.
I rewrote line 3 (the 2nd map) as:
map { [ $_, $_->get_value('ipHostNumber') ] }
and it worked.
If there is a cleaner way however - please let me know.
Otherwise, hopefully someone else can be helped by this post.
- Patrick
Cable, Patrick wrote:[color=darkred]
> Graham Barr wrote:
|
|
|
|
|