Re: sort an array unique.
- From: "Paul Lalli" <mritty@xxxxxxxxx>
- Date: 31 Jan 2007 06:39:09 -0800
On Jan 31, 9:23 am, "rajendra" <rajendra.pra...@xxxxxxxxxxxx> wrote:
Is there any command to sort an array uniquelly,something similar to
"sort -u" in unix.
Not built-in, but it's fairly easy to implement:
my %h = map { $_ => 1 } @unsorted_with_dups;
my @unsorted_uniques = keys %h;
my @sorted_uniques = sort @unsorted_uniques;
Or, you can remove all the intermediary steps:
my @sorted_unqiques = sort keys(%{{ map { $_ => 1 }
@unsorted_with_dups}});
Here's an example:
$ perl -le'
my @unsorted_with_dups = qw/d a e c b e d a c b e a b c e d/;
my @sorted_uniques = sort keys(%{{ map { $_ => 1 }
@unsorted_with_dups }});
print "@sorted_uniques\n";
'
a b c d e
Paul Lalli
.
- Follow-Ups:
- Re: sort an array unique.
- From: Robert 'phaylon' Sedlacek
- Re: sort an array unique.
- References:
- sort an array unique.
- From: rajendra
- sort an array unique.
- Prev by Date: Re: How to start some file as Perl script argument?
- Next by Date: Regular Expression help!
- Previous by thread: sort an array unique.
- Next by thread: Re: sort an array unique.
- Index(es):
Relevant Pages
|