http://qs321.pair.com?node_id=1077397

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,br> I am supposed to create a unique array based on a non-unique one. Problem is that I am not supposed to use hashes of built-in modules (I know, I know, but it's for educational purposes!!).
What I thought I could do is to sort the array and then somehow, since the elements would be "next" to each other, remove the duplicates.
my @array_q3=(); #create the array to store all words in the file open INFILE3, "<file"; while( my $line3 = <INFILE3>) { chomp $line3; push @array_q3, $line3; } close INFILE; #sort the array so we can identify the duplicates my @sorted_array_q3 = sort(@array_q3); print "@sorted_array_q3\n";

but my problem is that I can't understand how I can remove the duplicates now...
Tips?

Replies are listed 'Best First'.
Re: Create unique array --the hard way!
by hdb (Monsignor) on Mar 07, 2014 at 19:00 UTC

    How about losing the duplicates while sorting? Here is the quicksort implementation from Rosetta Code:

    sub quick_sort { my @a = @_; return @a if @a < 2; my $p = pop @a; quick_sort(grep $_ < $p, @a), $p, quick_sort(grep $_ >= $p, @a); } my @a = (4, 65, 2, -31, 0, 99, 83, 782, 1); @a = quick_sort @a; print "@a\n";

    (This is for sorting numbers.) If you now replace >= with > so you get

    quick_sort(grep $_ < $p, @a), $p, quick_sort(grep $_ > $p, @a);

    you will get a sorted array without duplicates.

Re: Create unique array --the hard way!
by kcott (Archbishop) on Mar 07, 2014 at 21:11 UTC

    Perhaps this will be suitable (for your class exercise):

    #!/usr/bin/env perl -l use strict; use warnings; my @initial = qw{q w e r t y q w e r t y}; print "Initial: @initial"; my $last = ''; my @unique = map { $last eq $_ ? () : ($last = $_) } sort @initial; print "Unique: @unique";

    Output:

    Initial: q w e r t y q w e r t y Unique: e q r t w y

    Update (alternative solution): You could even skip the creation of the unsorted array (your @array_q3):

    #!/usr/bin/env perl -l use strict; use warnings; my $last = ''; my @unique = map { $last eq $_ ? () : ($last = $_) } sort map { split +} <DATA>; print "Unique: @unique"; __DATA__ I am supposed to create a unique array I am not supposed to use hashes

    Output:

    Unique: I a am array create hashes not supposed to unique use

    -- Ken

      Many thanks to all of you guys!!
        I wonder, could this be done using the splice function as well? But how would you use it?
Re: Create unique array --the hard way!
by hazylife (Monk) on Mar 07, 2014 at 16:01 UTC
    foreach my $elem (@sorted) { push @uniq, $elem if !@uniq || $elem ne $uniq[-1]; } # OR my @uniq = shift @sorted; $uniq[-1] ne $_ && push @uniq, $_ for @sorted;
Re: Create unique array --the hard way!
by Laurent_R (Canon) on Mar 07, 2014 at 19:52 UTC
    A Perl one-liner doing this:
    $ perl -e 'my $prev; my @c = qw /a c d e r f e a e z e a /; print join + " ", grep { $_ ne $prev and $prev = $_;} sort @c' a c d e f r z
Re: Create unique array --the hard way!
by Anonymous Monk on Mar 07, 2014 at 15:53 UTC
    Ah, I did it using the following code:
    my @array_q3=(); #create the array to store all words in the file open INFILE3, "<file"; while( my $line3 = <INFILE3>) { chomp $line3; push @array_q3, $line3; } close INFILE3; #sort the array so we can identify the duplicates my @sorted_array_q3 = sort(@array_q3); my @unique_array; #iterate through the sorted array and check for unique elements foreach my $element ( @sorted_array_q3 ) { if ( ! grep( /$element/, @unique_array ) ) { push( @unique_array, $element ); } } #print the unique elements in the file clean.acc\n"; open OUTFILE3, ">unique_codes"; for (my $m=0; $m<=$#unique_array; $m++) { print OUTFILE3 $unique_array[$m]."\n"; } close OUTFILE3;

    My problem here is with grep, since I seem to miss one element... The element has the code J00148 and apparently there is a conflict with another element, namely AJ001487.
    What must I add in my grep conditional?
      Got it!
      if ( ! grep ( /^$element$/, @unique_array ) )

        Great that you found a solution yourself

        Since you didn't follow your own idea of using sorting to identify doubles the sort step is now useless.

        To follow your original plan you would have to do something like this:

        my $previous=""; foreach my $element ( @sorted_array_q3 ) { if ( $element ne $previous ) { push( @unique_array, $element ); } $previous= $element; }