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

Dear Monks, How to delete the Array of array duplication value in perl? Ex:
@AoD = ([11,12,22 ],[11,12,22 ],[13,15,18]);
Result like:
@AoD = ([11,12,22 ],[13,15,18]);

Replies are listed 'Best First'.
Re: Eliminate Array of array duplication
by ikegami (Patriarch) on May 19, 2011 at 07:37 UTC

    For each reference, create a signature string. Two references you consider different must have different signatures. Two references you consider identical must have identical signatures. Use a hash keyed by these signatures to identify duplicates.

    my %seen; @AoD = grep !$seen{ join(',', @$_) }++, @AoD;
      I would agree with ikegami's post above, the only thing I'd add it that you should probably sort the array before using it as a key, if [1, 2, 3] , [3, 2, 1] and [2, 1, 3] are considered identical.
      my %seen; @AoD = grep !$seen{ join(',', (sort {$a<=>$b} @$_ )) }++, @AoD;
      print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."

        Indeed. If one desires for [1,2,3] and [3,2,1] to be considered identical, they must have identical signatures, and that can be achieved by sorting the numbers.

        I doubt that's going to be a common desire, but if it happens, it's probably cleaner to normalise the data before finding the dups.

        # Normalise the data. @$_ = sort { $a <=> $b } @$_ for @AoD; # Filter out duplicates. my %seen; @AoD = grep !$seen{ join(',', @$_) }++, @AoD;
Re: Eliminate Array of array duplication
by John M. Dlugosz (Monsignor) on May 19, 2011 at 16:42 UTC
Re: Eliminate Array of array duplication
by senthilkumarperl (Novice) on May 19, 2011 at 09:44 UTC

    Hi,

    @AoD = (11,12,22 ,11,12,22 ,13,15,18); foreach my $v1(@AoD) { print $v1."\n"; } print "======\n"; my %hash = map { $_, 1 } @AoD; my @AoD = keys %hash; print "======\n"; foreach my $v(@AoD) { print $v."\n"; }

    By the above code We can remove duplicate in array

      His bad formatting mislead you. He wants to turn

      @AoD = ([11,12,22], [11,12,22], [13,15,18]);

      into

      @AoD = ([11,12,22], [13,15,18]);

      Ignoring that, the advantage of previously mentioned

      my %seen @AoD = grep !$seen{$_}++, @AoD;

      over your solution

      my %seen = map { $_ => 1 } @AoD; @AoD = keys(%seen);

      is that the former maintains the order of the elements it keeps.