Henry Law <
ne...@lawshouse.org> writes:
> I have an array of arrayrefs with this structure:
>
> @array = ( [ '20100403', 'text', 'account code' ],
> [ '19990503', 'text', 'other account code' ],
> ... etc )
>
> I want to sort by account code and then by date.
Do you want elements with the same account code and different dates
grouped together, or vice versa?
> I read in various
> places that the following will do the trick:
>
> @array = sort { $a->[2] cmp $b->[2] || $a->[0] <=> $b->[0] } @array;
>
> But I'm not getting the sort order I want. I know that the "sort"
> function requires a three-valued return from the block, -1, 0 or +1;
The function has to return a value that's less than, equal to, or
greater than zero. It doesn't have to be -1, 0, or +1 (though of course
it can be, and "<=>" and "cmp" do so unless you use "<=>" with NaNs).
See "perldoc perlop".
> but
> the "||" operator won't do that; it returns TRUE or FALSE.
>
> Is that understanding correct? Is that the reason that I'm not getting
> the order I want?
No. Quoting "perldoc perlop":
The "||", "//" and "&&" operators return the last value evaluated
(unlike C's "||" and "&&", which return 0 or 1).
(And note that Perl doesn't have TRUE and FALSE constants.)
I don't know what the problem is. I'd probably use "or" rather than
"||", or add parentheses, just for greater clarity, but "cmp" and "<=>"
bind more tightly than either "||" or "or", so that's not the issue.
If you don't get an answer from someone else, try showing us some sample
data, the order you expect, and the order you're getting.
--
Keith Thompson (The_Other_Keith)
Keith.S.T...@gmail.com
Working, but not speaking, for Philips
void Void(void) { Void(); } /* The recursive call of the void */