Uri Guttman <[EMAIL PROTECTED]> writes:
>>>>>> "RA" == Russ Allbery <[EMAIL PROTECTED]> writes:
>   RA> Uri Guttman <[EMAIL PROTECTED]> writes:

> map { $_->[0] } sort { compare($a->[1], $b->[1]) } map { [$_, f($_)] } data
>                                ^^^^^^^   ^^^^^^^

>   RA> Then you need to look at f and compare a little closer, since it's in
>   RA> there.

> and there is only extracted key being compared to another at the same
> level, not multiple key levels. think about sorting by state and THEN
> town. you can't do that with $a and $b and one f().

Yes.  You can.

Don't assume $a->[1] is a simple scalar.  What prevents f() from returning
an array ref?

> so you need multiple compare ops and multiple f()'s.

No, you don't.

> the point is that you have to generate the ladder compare code as well
> as the calls to your f()'s.

Yes, you have to write the comparison and data manipulation function for
Perl; Perl isn't going to be able to figure it out for itself.  But that's
true regardless of the sorting method; you're always going to have to tell
Perl what the keys are and how to compare them.

You have to write slightly more code if you separate the extraction
function f() from the comparison function compare() since if the key
structure is complex, f() has to build a data struction that compare()
takes apart.  That makes the memoizing approach superior.

>   RA> Without creating a function to extract the key, you can't sort in
>   RA> Perl at all.  sort { $a <=> $b } contains two functions to extract
>   RA> the keys.

> huh? $a and $b are not functions but aliases the the current pair of
> keys (at the primary key level).

Is sub { $a } a function?  $a is equivalent to that.  One way to look at
this is that Perl lets you simplify the function if all you need is the
basic data unit.

> i don't seen any functions in what you show there. you don't need a
> function or even an ST to sort complex records.

{ $a <=> $b } is a function.  (Well, it's a code block, but the difference
is quibbling.)

My point is that writing functions isn't nearly as complicated as you make
it sound.  Almost every time I write a sort, map, or grep in Perl, I write
a function.

-- 
Russ Allbery ([EMAIL PROTECTED])             <http://www.eyrie.org/~eagle/>

Reply via email to