in reply to Re^2: Sorting in Perl
in thread Sorting in Perl
Those don't actually sort a list, they remove duplicate entries from it.
Another variant on the same theme is using a slice, e.g.:
my @array = (10 20 20 20 30 40 40 40 50 50 50); my %hash; @hash{ @array } = (); say keys %hash;
But what I'd personally recommend is using the uniq function from List::MoreUtils (or List::AllUtils if you can't be bothered to remember whether functions are List:Util or List:MoreUtils).
It avoids reinventing the wheel (fun as it is to do so as an academic exercise), it's shorter, and it makes it crystal clear what's going on, which will benefit whoever reads your code at a later time.
EDIT: changed @hash{ @array } = 0 to @hash{ @array } = (); thanks for the suggestion to choroba.
|
|---|