in reply to Return a Unique Array

Using hashes can take up a lot of memory, especially with long lists. Here's one that acts like sort -u :
my @list = sort(@list); my @unique; my $i=0; $unique[0] = $list[0]; foreach my $item(@list) { unless($item eq $unique[$i]) { push(@unique,$item); $i++; } }
@unique is now a sorted unique list.

Edit kudra, 2001-09-10 Added code tags

Replies are listed 'Best First'.
RE: RE: Return a Unique Array
by btrott (Parson) on Mar 23, 2000 at 22:20 UTC
    That's nice. Although it is slower than using a hash. Comparing your code against this routine:
    sub sort_unique_hash { my %hash; @hash{@_} = (); return sort keys %hash; }
    gives (where "foreach" is your code and "hash" is the above):
    Benchmark: timing 100000 iterations of foreach, hash... foreach: 14 secs (13.50 usr 0.00 sys = 13.50 cpu) hash: 6 secs ( 5.85 usr 0.00 sys = 5.85 cpu)
    This is with:
    @list = qw/foo bar baz foo quack bar baz/;
    The difference is even more marked if you have rather odd arrays that you want to sort -u. For example, running the code against this array:
    @list = ('foo') x 1000;
    gives
    Benchmark: timing 1000 iterations of foreach, hash... foreach: 12 secs (11.82 usr 0.00 sys = 11.82 cpu) hash: 1 secs ( 0.88 usr 0.00 sys = 0.88 cpu)
    Presumably because your code sorts the list before it selects only the unique elements, and the hash approach only sorts the unique list--so you're sorting 1000 elements, and the hash approach just sorts 1.

    Just something to think about.

Re^2: Return a Unique Array
by Anonymous Monk on Apr 10, 2014 at 16:16 UTC
    Should be: my $i=1 otherwise you overwrite the first element and are then missing the first element.
      Oops, no it shouldn't, my mistake. $i=0 is correct.