Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

How do I find unique Array in Array of Arrays?

( #489796=categorized question: print w/ replies, xml ) Need Help??
Contributed by monkfan on Sep 07, 2005 at 08:31 UTC
Q&A  > arrays


Description:

Given this:
my @AoA = ( ['a','b','c'], ['a','b','c'], ['a','b','d'], ['a','b','d'], );
I wish to return simply this:
my @uAoA = ( ['a','b','c'], ['a','b','d'], );
From the original posting in SoPW, one can find many answers. One of them is (by TedPride), seems to me to be the most compact of them all:
use strict; use warnings; use Data::Dumper; my @AoA = ( ['a','b','c'], ['a','b','c'], ['a','b','d'], ['a','b','d'], ); my (%h, @uAoA); for (@AoA) { push @uAoA, $_ if !$h{join $;, @$_}++; } print Dumper \@uAoA;

Answer: How do I find unique Array in Array of Arrays?
contributed by bart

There's a likely cheaper solution to stringify an array of strings (no nested arrays) than using Data::Dumper: if you first quotemeta every string and then join by any \W character (except '\'), then you can uniquely recognize any combination of characters in the arrays, because the join string will not have a prepending backslash, and every \W character in the data will.

In addition, I'm using fewer extra data structures than in the other proposed solutions.

my %seen; my @aoa = grep { not $seen{join " ", map quotemeta, @$_}++ } @AoA;
Answer: How do I find unique Array in Array of Array?
contributed by jdporter

In order to detect duplicates, one needs a way of testing equality. For non-scalar data structures, this can be tricky, or at least application dependent.

As a generic solution, one can simply stringify each datastructure and compare using string equality (eq). This technique probably breaks badly when any of the contents are objects or functions or other exotic beasts. For strings and numbers, it works pretty well.

Here, I use Data::Dumper for stringification:

my @a = ( ['a','b','c'], ['a','b','c'], ['a','b','d'], ['a','b','d'], ); my @b = do { use Data::Dumper; my %seen; map { $_->[0] } grep { !$seen{$_->[1]}++ } map { [ $_, Dumper($_) ] } @a };
Answer: How do I find unique Array in Array of Array?
contributed by mohan123

use strict; use warnings; use Data::Dumper; my @AoA = (['a','b','c'], ['a','b','c'], ['a','b','d'], ['a','b','d']); my %temp=(); my @aoa=map{[split $;,$_]}grep{++$temp{$_}<2} map {join $;,@$_} @AoA; print Dumper \@aoa;

Janitored by Corion: Added formatting, code tags, as per Writeup Formatting Tips

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others cooling their heels in the Monastery: (5)
    As of 2014-12-29 04:49 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (184 votes), past polls