[ABE.pm] sorting an AoA
Walt Mankowski
waltman at pobox.com
Wed Jul 26 13:20:40 PDT 2006
On Wed, Jul 26, 2006 at 04:06:32PM -0400, Faber J. Fedor wrote:
> On 26/07/06 15:56 -0400, Walt Mankowski wrote:
> > On Wed, Jul 26, 2006 at 03:24:59PM -0400, Faber J. Fedor wrote:
> > > How do I get @array1 sorted into @array2?
> >
> > This seems to work:
> >
> > my @array2 = sort {$a->[1] <=> $b->[1]} @array1;
>
> You're right. I obviously don't understand what's going on here (that's
> a Schwartzian transform, right?) because I thought it was returning a
> one-dimensional array.
No, it's a simple sort comparison function. Sort takes pairs of
values from @array1 and passes them to the block of code inside {} for
comparison. The values are designated by $a and $b. $a and $b are
array references, and we want to refer to the second item in the
arrays, so we say $a->[1] and $b->[1]. The <=> thing, (aka the
"spaceship operator") is mainly used with sort. It returns -1, 0, or
1 depending on whether the left argument is less than, equal to, or
greater than the right argument.
Walt
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://mail.pm.org/pipermail/abe-pm/attachments/20060726/76d1b727/attachment.bin
More information about the ABE-pm
mailing list