in reply to Sorting an Array

Thanks everyone. The sorting script worked. Not sure I understand the last comment about using my %entry. I'm sure this may help in speed of script. So I would love some further clarification on that. Also I made a slight change in sorting script to get a descending order. here is the final code.
for (@{ $data->{'mail-item'} }) { my %entry; $entry{job} = $_->{'mail-jobnum'}; $entry{product} = $_->{'mail-prodnum'}; $entry{statement_date} = $_->{'mail-statement-date'}; push(@statements, {%entry}); } @statements = sort by_date @statements; $c->stash->{statements} = \@statements; sub by_date { my $dateA = $a->{statement_date}; my $dateB = $b->{statement_date}; $dateA =~ s!(\d\d)/(\d\d)/(\d\d\d\d)!$3$1$2!; $dateB =~ s!(\d\d)/(\d\d)/(\d\d\d\d)!$3$1$2!; $dateB cmp $dateA; }

Replies are listed 'Best First'.
Re^2: Sorting an Array
by AppleFritter (Vicar) on Jul 31, 2014 at 18:35 UTC

    Not sure I understand the last comment about using my %entry. I'm sure this may help in speed of script. So I would love some further clarification on that.

    This refers to the for loop where you're pushing entries onto your @statement array. You're explicitely constructing a named hash, %entry, each time, but this isn't necessary; you can just as well do this:

    for (@{ $data->{'mail-item'} }) { push @statements, { job => $_->{'mail-jobnum'}, product => $_->{'mail-prodnum'}, statement_data => $_->{'mail-statement-date'} }; }

    Or, since you only have one statement in your loop body now, you can even use for as a statement modifier:

    push @statements, { job => $_->{'mail-jobnum'}, product => $_->{'mail-prodnum'}, statement_data => $_->{'mail-statement-date'} } for (@{ $data->{'mail-item'} });

    Although I personally consider this less readable than the "regular" loop above.