in reply to Remove similar elements in both the array

my %in_a; my %in_b; undef @in_a{@a}; undef @in_b{@b}; @a = grep {not exists $in_b{$_}} @a; @b = grep {not exists $in_a{$_}} @b;
Update: I guess that I do need the braces after all, as davis noted I didn't run the code. Sorry.

Replies are listed 'Best First'.
Re^2: Remove similar elements in both the array
by davis (Vicar) on Mar 08, 2005 at 09:58 UTC
    @a = grep not exists $in_b{$_}, @a; @b = grep not exists $in_a{$_}, @b;

    According to my perl:

    Not enough arguments for grep at j.pl line 14, near "@a;" Execution of j.pl aborted due to compilation errors.
    I think you want:
    #!/usr/bin/perl use warnings; use strict; use Data::Dumper; my @a = (2, 6.5 ,4, 3, 2, 7.5, 4.0); my @b = (2, 6.52 ,4.5, 7.05, 2.0, 4.1, 3); my %in_a; my %in_b; undef @in_a{@a}; undef @in_b{@b}; @a = grep {not exists $in_b{$_}} @a; @b = grep {not exists $in_a{$_}} @b; print Dumper(\@a); print Dumper(\@b);

    davis
    It wasn't easy to juggle a pregnant wife and a troubled child, but somehow I managed to fit in eight hours of TV a day.