in reply to Re: Sorting in Perl
in thread Sorting in Perl

there are many different ways in perl to sort numbers, i found those here
use strict; use Data::Dumper; #Remove duplicates from array. my @array = qw/10 20 20 20 30 40 40 40 50 50 50/; print "\n Duplicate array: @array"; ##1) Good my %hash; $hash{$_} = 0 for (@array); # $hash{$_} = () for (@array); #You can do this also my @final = keys (%hash); print "\n Unique Array: @final"; print "\n"; ##2) Best of all my %hash = map { $_ , 1 } @array; my @uniq = keys %hash; print "\n Uniq Array:", Dumper(\@uniq);
Source: Different ways to remove duplicates from array - sort and uniq in perl

Replies are listed 'Best First'.
Re^3: Sorting in Perl
by AppleFritter (Vicar) on Oct 16, 2015 at 10:47 UTC

    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.