dalden (1) [Avatar] Offline

On page 45 and the beginning of 46 you list three separate pieces of code

my @CD_and_year = map { [$_->{year}, $_] } @CDs;


my @sorted_CD_and_year = sort { $a->[0] <=> $b->[0] } @CD_and_year


my @CDs_sorted_by_year = map { $_=>[1] } @sorted_CD_and_year

in the middle of page 46 you put these three stages together with different indexing on the second and third lines:

my @CD_and_year = map { [$_->{year}, $_] } @CDs;
my @sorted_CD_and_year = sort { $a->[1] <=> $b->[1] } @CD_and_year
my @CDs_sorted_by_year = map { $_=>[0] } @sorted_CD_and_year

Is this an error?


davorg (1) [Avatar] Offline
Re: possible errata page 45 and 46
I think you mean pages 44 and 45.

It is confusing, I agree, and I shouldn't have changed the order like that in the middle of an explaination. But both versions are correct.

In the first version the elements in the @CD_and_year array are two element arrays with the year in element 0 and the whole record in element 1. Therefore we use element 0 for sorting and extract the whole record from element 1 at the end.

In the seond version I (unintentionally) reversed the way the @CD_and_year array is created so the two element arrays have the whole record in element 0 and the year in element 1. Therefore the sorting and extraction lines also need to change.

So, yes, it should probably be corrected. But it's not actually wrong smilie