I agree with Matt that you are probably looking for `qsort`.
As to what
@e = pdl(3,2,6,4,8,6);
@r = sort{$a <=> $b} @e;
is doing, its working perfectly; its just not doing what you mean. @e
is a one element Perl-level array, its one element is a PDL object.
Any sort on a one element array will return the same order, what else
could it do.
You have to remember that a PDL object is just another scalar in
Perl's eyes, as are all objects.
Here is another example
@e = (pdl(3,2,6,4,8,6), pdl(5,6,2,1));
@r = sort{$a <=> $b} @e;
Here @e has two PDL object. When you sort objects numerically ( using
<=> ), what you will actually sort on is not their contents, but their
address in memory.
The take-away message is this: PDL overloads many of the Perl
operators, and it can feel like PDL and Perl are fully integrated, but
in truth a PDL object is still an object, that is a scalar reference
with methods and overloads. PDL tries to Do What You Mean when it can,
this is not one of those times.
Joel
On Thu, Feb 2, 2012 at 1:31 AM, Matthew Kenworthy
<[email protected]> wrote:
> Let's try searching the PDL documentation with 'pdldoc -a' where '-a'
> means 'search for this string':
>
> bash$ pdldoc -a sort
> init The init method initializes a new instance of INTERP.
> It needs as input an interpolation type and two piddles
> holding the x and y values to be interpolated. The GSL
> routines require that x be monotonically increasing and
> a quicksort is performed by default to ensure that. You
> can skip the quicksort by passing the option {Sort =>
> 0}.
> qsort Quicksort a vector into ascending order.
> qsorti Quicksort a vector and return index of elements in
> ascending order.
> qsortvec Sort a list of vectors lexicographically.
> qsortveci Sort a list of vectors lexicographically, returning the
> indices of the sorted vectors rather than the sorted
> list itself.
>
> It looks as if you're after 'qsort', so let's try that:
>
> Module PDL::Ufunc
> qsort
> Signature: (a(n); [o]b(n))
>
> Quicksort a vector into ascending order.
>
> print qsort random(10);
>
> Bad values are moved to the end of the array:
>
> pdl> p $b
> [42 47 98 BAD 22 96 74 41 79 76 96 BAD 32 76 25 59 BAD 96 32 BAD]
> pdl> p qsort($b)
> [22 25 32 32 41 42 47 59 74 76 76 79 96 96 96 98 BAD BAD BAD BAD]
>
>
> Cheers,
>
> Matt
>
>
> On Thu, Feb 2, 2012 at 8:09 AM, MARK BAKER <[email protected]> wrote:
>>
>>
>> ________________________________
>> From: MARK BAKER <[email protected]>
>> To: "[email protected]" <[email protected]>
>> Sent: Wednesday, February 1, 2012 10:43 PM
>> Subject: how to sort a piddle ???
>>
>> I have been looking over PDL and trying to learn every thing inside and out
>> and I came across this, how do I sort a piddle ???
>> is there a special function for sorting a piddle
>>
>> I tried this it doesn't work ...
>>
>> pdl> @e = pdl(3,2,6,4,8,6);
>> pdl> @r = sort{$a <=> $b} @e;
>> pdl> p @r;
>> [3 2 6 4 8 6]
>> pdl> why didnt this sort ?
>>
>> Now I can think of about 6 different ways to sort this
>> but they are tedious maybe someone can show me a simpler way !
>>
>> Thanks
>>
>> -Mark R Baker
>> [email protected]
>>
>>
>>
>>
>> _______________________________________________
>> Perldl mailing list
>> [email protected]
>> http://mailman.jach.hawaii.edu/mailman/listinfo/perldl
>>
>
>
>
> --
> Matthew Kenworthy / Assistant Professor / Leiden Observatory
> Niels Bohrweg 2 (#463) / P.O. Box 9513 / 2300 RA Leiden / NL
>
> _______________________________________________
> Perldl mailing list
> [email protected]
> http://mailman.jach.hawaii.edu/mailman/listinfo/perldl
_______________________________________________
Perldl mailing list
[email protected]
http://mailman.jach.hawaii.edu/mailman/listinfo/perldl