lampros21_7 has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks. I have an array which by using a regexp i remove the elements i don't want. The problem is that i also want to remove the elements in the same position from another array whenever i remove an element from the first array.

For example, i have @array1 and @array2. By using:  @array1 = grep(!some regexp, @array1);

if the regexp will remove the 3rd element from @array1 how can i remove the 3rd element from @array2 as well?

I don't really know in which positions my elements are that i will want to remove. Thanks for your help in advance

Replies are listed 'Best First'.
Re: Use grep with two arrays
by QM (Parson) on Feb 14, 2006 at 02:04 UTC
    my @keepers; foreach my $i (0..$#array1) { push @keepers, $i if $array1[$i] =~ /keep these/; } @array1 = @array1[@keepers]; @array2 = @array2[@keepers];

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

      Just for giggles, here's that using grep:

      my @keepers = grep { $array1[$_] =~ /keep these/ } 0 .. $#array1; @array1 = @array1[@keepers]; @array2 = @array2[@keepers];
        And to avoid the temp variable — not really a valid goal, but kind of fun — and not repeat the grep:
        my @a1 = qw'only keep ones that have es'; my @a2 = qw'art bart cart dart eart good'; @a1 = @a1[sort {$a <=> $b} grep {$a1[$_] =~ /e/ or do { splice @a2, $_ +, 1; 0 } } reverse 0..$#a1]; print "@a1\n@a2\n";
        It gets a little complicated, mostly because of the gyrations necessary to splice out the right element after others have been removed.

        Caution: Contents may have been coded under pressure.
        So to be inefficient and avoid the temp variable (untested):
        @array1 = @array1[+grep { $array1[$_] =~ /keep these/ } 0 .. $#array1 ]; @array2 = @array2[+grep { $array1[$_] =~ /keep these/ } 0 .. $#array1 ];

        -QM
        --
        Quantum Mechanics: The dreams stuff is made of

Re: Use grep with two arrays
by davidrw (Prior) on Feb 14, 2006 at 02:21 UTC
    i like QM's solution .. though I have to also question OP w/why store your data in 2 arrays?

    Hashing (be aware of uniqueness and sorting issues) is one option:
    my %hash; @hash{ @array1 } = @array2; my @array1 = grep( ! /foo/, keys %hash ); my @array2 = map { $hash{$_} } @array1;
    Or even better/more robust, build a different data structure:
    my @array = grep $_->[0] !~ /someRE/, map { [ $array1[$_], $array2[$_] ] } 0 .. $#array1; @array1 = map { $_->[0] } @array; @array2 = map { $_->[1] } @array;
Re: Use grep with two arrays
by Praveen (Friar) on Feb 15, 2006 at 07:23 UTC
    Try this
    @ary1= (1,2,3,4,3); @ary2= (a,b,c,d,u); print "Before\n"; print "@ary1","\n"; print "@ary2","\n"; $cnt=0; foreach (@ary1) { push(@new_ary,$cnt), if($_ =~ /3/); $cnt++ } splice(@ary1,$_,1,"undef"), for(@new_ary); splice(@ary2,$_,1,"undef"), for(@new_ary); @ary1 = grep{!/undef/} @ary1; @ary2 = grep{!/undef/} @ary2; print "\nAfter\n"; print "@ary1","\n"; print "@ary2","\n";
    Output:
    Before
    1 2 3 4 3
    a b c d u
    After
    1 2 4
    a b d