### Unique rows in Array of array

by DreamT (Pilgrim)
 on Sep 03, 2012 at 12:35 UTC Need Help??
DreamT has asked for the wisdom of the Perl Monks concerning the following question:

Hi!
I have an array where every row is an array with three values. I want to remove the rows where the three values are equal. How do I do it? (I've looked around but can't find any solution that applies on an AoA, only on scalars in an array)

Replies are listed 'Best First'.
Re: Unique rows in Array of array
by choroba (Bishop) on Sep 03, 2012 at 13:05 UTC
To filter an array one usually uses grep. You have to dereference the inner arrays to get to their members:
```#!/usr/bin/perl
use warnings;
use strict;

use Data::Dumper;

my @array = ([ 1,  2,  3],
[10, 10, 10],
[ 1,  1,  5]);

my @nonequal = grep \$_->[0] != \$_->[1]
||
\$_->[2] != \$_->[0],
@array;

print Dumper \@nonequal;
لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
Re: Unique rows in Array of array
by johngg (Abbot) on Sep 03, 2012 at 16:55 UTC

choroba gives you a solution for the problem as you present it but, more generally, if you have arrays with longer rows the number of conditions inside the grep becomes unwieldy. A nested grep solution can overcome this.

```\$ perl -E '
> @arr = (
>    [ 1, 3, 6 ],
>    [ 8, 8, 8 ],
>    [ 3, 9, 0 ],
>    [ 1, 4, 5, 3, 6, 9, 4, 2 ],
>    [ 2, 2, 2, 2, 2, 2, 2 ],
>    );
>
> @neq =
>    grep {
>       \$zero = \$_->[ 0 ];
>       grep { \$zero != \$_ } @\$_[ 1 .. \$#{ \$_ } ]
>       } @arr;
>
> say qq{@\$_} for @neq;'
1 3 6
3 9 0
1 4 5 3 6 9 4 2

I hope this is of interest.

Cheers,

JohnGG

Create A New User
Node Status?
node history
Node Type: perlquestion [id://991421]
Approved by Corion
help
Chatterbox?
 [Discipulus]: good morning everyone [Corion]: Hi Discipulus!

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2018-01-22 07:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How did you see in the new year?

Results (232 votes). Check out past polls.

Notices?