in reply to Re: Removing duplicates in Array of Array
in thread Removing duplicates in Array of Array

The sad story is I have a piece of code that can remove duplicates using grep:
my @unique_terminal_id = grep { !($seen_values{$_->[4]}++) } @temp_ope +n_single; my %seen_values; my @unique_manufacturer_model = grep { !($seen_values{$_->[9]}{$_->[10 +]}++) } @unique_terminal_id; my %seen_values; my @unique_manufacturer_model_version = grep { !($seen_values{$_->[9]} +{$_->[10]}{$_->[11]}++) } @unique_terminal_id;
But I discover that updating @unique_manufacturer_model also changes @unique_manufacturer_model_version. I was told that this is due to the fact that I am storing reference (cross-referencing trap). I was told to use Storeable(dclone), but I ca'nt figure out how to apply. So I was thinking if there could be a longer (inefficient) way of getting around this.

Replies are listed 'Best First'.
Re^3: Removing duplicates in Array of Array
by GrandFather (Saint) on Sep 04, 2008 at 04:58 UTC

    Ah, so the bigger picture is that you need a deep copy of the elements from the original array? That is, at present you fall foul of:

    use strict; use warnings; my %seen; my @array = (["1", "1", "3"], ["1", "5", "6"], ["1", "1", "3"]); my @unique = grep {! $seen{$_->[1]}++} @array; print "@$_\n" for @array; print "\n"; map {$_++} @$_ for @unique; print "@$_\n" for @array; print "\n";

    Prints:

    1 1 3 1 5 6 1 1 3 2 2 4 2 6 7 1 1 3

    where you wanted @array unaffected by the manipulation of the contents of @unique. If you know that you are dealing with an AoA then you can:

    use strict; use warnings; my %seen; my @array = (["1", "1", "3"], ["1", "5", "6"], ["1", "1", "3"]); my @unique = map {[@$_]} grep {! $seen{$_->[1]}++} @array; print "@$_\n" for @array; print "\n"; map {$_++} @$_ for @unique; print "@$_\n" for @array; print "\n"; print "@$_\n" for @unique; print "\n";

    Prints:

    1 1 3 1 5 6 1 1 3 1 1 3 1 5 6 1 1 3 2 2 4 2 6 7

    Note the map used with the result from grep to copy the elements of each unique row? So actually rather than using neither grep nor map, what you really needed was to use both grep and map!


    Perl reduces RSI - it saves typing

      ... or just the map (no golf; grep was excessive) ...

      my @unique = map { ! $seen{ $_->[1] }++ ? [ @{ $_ } ] : () } @array;

        In the general case that is quite true, but in the context of the OP using both was virtually required. ;)


        Perl reduces RSI - it saves typing
      Hey GrandFather, thanks for your help. Using Map together with Grep does the trick. Thanks so much for your help. You are really the GrandFather of Perl...