in reply to Remove Array Duplicates from Array of Arrays

Here’s one approach: convert each inner array into a single string, and store it in a hash for future lookup:

use strict; use warnings; use Data::Dump; my @MyArrayOfArray = ( [ "hello", "day", "sun and fun" ], [ 2, "okay", "may" ], [ "hello", "day", "sun and fun" ], [ 2, "okay", "may" ], [ "hello", "sun and fun", "day" ], [ 2, "okay", "may" ], ); my (%hash, @AoA2); for (@MyArrayOfArray) { my $key = join '', @$_; push @AoA2, $_ unless exists $hash{ $key }; ++$hash{ $key }; } dd \@AoA2;

Output:

18:32 >perl 1924_SoPW.pl [ ["hello", "day", "sun and fun"], [2, "okay", "may"], ["hello", "sun and fun", "day"], ] 18:32 >

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Replies are listed 'Best First'.
Re^2: Remove Array Duplicates from Array of Arrays
by AnomalousMonk (Archbishop) on Sep 01, 2018 at 09:13 UTC
    my $key = join '', @$_;

    Note that join-ing with the empty string means that, e.g.,  [ 'hello', 'sun and fun', 'day' ] cannot be distinguished from the arguably different subarray  [ 'hello', 'sun and funday' ] (among other permutations):

    c:\@Work\Perl\monks\Anonymous Monk>perl -wMstrict -le "use Data::Dump; ;; my @MyArrayOfArray = ( [ 'hello', 'sun and fun', 'day' ], [ 2, 'okay', 'may' ], [ 'hello', 'sun and funday' ], [ 2, 'okay', 'may' ], ); ;; my (%hash, @AoA2); ;; for (@MyArrayOfArray) { my $key = join '', @$_; push @AoA2, $_ unless exists $hash{ $key }; ++$hash{ $key }; } ;; dd \@AoA2; " [["hello", "sun and fun", "day"], [2, "okay", "may"]]
    A join string that is guaranteed not to appear in any text will avoid this. Here I use  $; (see perlvar), the default value of which just happens to work in this particular case:
    c:\@Work\Perl\monks\Anonymous Monk>perl -wMstrict -le "use Data::Dump; ;; my @MyArrayOfArray = ( [ 'hello', 'sun and fun', 'day' ], [ 2, 'okay', 'may' ], [ 'hello', 'sun and funday' ], [ 2, 'okay', 'may' ], ); ;; my (%hash, @AoA2); ;; for (@MyArrayOfArray) { my $key = join $;, @$_; push @AoA2, $_ unless exists $hash{ $key }; ++$hash{ $key }; } ;; dd \@AoA2; " [ ["hello", "sun and fun", "day"], [2, "okay", "may"], ["hello", "sun and funday"], ]

    Update: Slightly more concisely:

    c:\@Work\Perl\monks\Anonymous Monk>perl -wMstrict -le "use Data::Dump; ;; my @MyArrayOfArray = ( [ 'hello', 'sun and fun', 'day' ], [ 2, 'okay', 'may' ], [ 'hello', 'sun and funday' ], [ 2, 'okay', 'may' ], ); ;; my @AoA2 = do { my %seen; grep ! $seen{ join $;, @$_ }++, @MyArrayOfArray; }; ;; dd \@AoA2; " [ ["hello", "sun and fun", "day"], [2, "okay", "may"], ["hello", "sun and funday"], ]
    (Update: Posted this update before I saw Dallaylaen's post with essentially the same idea.)


    Give a man a fish:  <%-{-{-{-<