Re: sort an array unique.



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

.



Relevant Pages

  • Re: updmap input file help
    ... options in Unix have the general form ... But in this sort of situation, ... programmers are usually very unhelpful. ... The Fibonacci series is infinite! ...
    (comp.text.tex)
  • Re: I dig stumperings.....
    ... infinite impregnability where no-one's troubled by buses. ... suggesting both within and among the keys. ... but one went sort of like this. ...
    (talk.religion.buddhism)
  • Re: a question for sorting keys in Map
    ... I have a Map, actually a TreeMap, which will automatically sort the keys ... You can write your own Comparator object which implements the Compare ... strings so that X10 comes after X2 instead of before it. ... Unix ls command sort file names the way you want your strings to sort. ...
    (comp.lang.java.programmer)
  • Re: sorting DBM hash
    ... a DBM Hash. ... The first sort routine prints out keys and value. ... An anonymous sub routine is used to hold sub routines ...
    (perl.beginners)
  • Re: Good Dungeon Mapping e-Tool?
    ... We don't have any VB of any sort running around. ... but on a multi-core CPU ... don't involve threading, even if they are evidently more complex. ... My last organization, I was doing a *lot* of UNIX stuff (Digital Tru64, ...
    (rec.games.frp.dnd)