Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Unique rows in Array of array

by DreamT (Pilgrim)
on Sep 03, 2012 at 12:35 UTC ( [id://991421]=perlquestion: print w/replies, xml ) 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 (Cardinal) 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 (Canon) 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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://991421]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-26 08:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found