in reply to Question about map function [ANSWERED]

Why are you doing
my @records = map { $_->[0] } sort $sort_routine map { [ $_, $_->{'title'}, $_->{'cleandate'} ] } @_;
instead of
my @records = sort $sort_routine @_;

You're slowing things down and wasting memory.

Also,

($opt_g =~ /^t/i) ? #flag setting sort order ($sort_routine = \&sort_by_title) : ($sort_routine = \&sort_by_date);

is a complicated way of doing

$sort_routine = ( $opt_g =~ /^t/i ? \&sort_by_title : \&sort_by_date );

I've never seen a situation where it made sense to use an assignment inside of the conditional operator (?:).

So, if your date format is really YYYY.MM.DD (and not YYYY.M.D), you get

sub sort_records { my $sort_routine ( $opt_g =~ /^t/i ? sub { $a->{title} cmp $b->{title} } : sub { $a->{date} cmp $b->{date} } ); return sort $sort_routine @_; }

Replies are listed 'Best First'.
Re^2: Question about map function [ANSWERED]
by automandc (Acolyte) on Nov 19, 2008 at 03:13 UTC
    Thanks, this is all very helpful. My perl is very rusty, but slowly coming back. I had just figured out the silliness of doing assignment inside the conditional, and could have saved myself a headache if I had read your response first!

    My sub routines actually had alternative sorts, so they checked the first choice (title or date) and then the second one. I have also now figured out (again, the hard way) that regular string comparison works fine for my date format (not sure why I thought differently). Sigh, live and learn. But my code is getting better.