in reply to Re^2: Use grep with two arrays
in thread Use grep with two arrays

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

Replies are listed 'Best First'.
Re^4: Use grep with two arrays
by graff (Chancellor) on Feb 15, 2006 at 01:03 UTC
    Um, I think this suggestion, as you originally posted it, will not work as you intend/expect:
    @array1 = @array1[+grep { $array1[$_] =~ /keep these/ } 0 .. $#array1 ]; @array2 = @array2[+grep { $array1[$_] =~ /keep these/ } 0 .. $#array1 ];
    When you process @array2, @array1 is presumably shorter than it used to be -- the elements of the two arrays are no longer in parallel (assuming they were parallel before the first grep operation). You'll be getting the wrong elements left behind in @array2 (only the last elements of @array2 will ever be removed).
      Yes, of course. Put it's supposed to DWIM, even if I ask the impossible, right? ;)

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

Re^4: Use grep with two arrays
by mrborisguy (Hermit) on Feb 14, 2006 at 18:44 UTC

    And to then avoid reproducing code (which probably negates the advantages of not using a temp variable) (untested):

    @array1 = @array1[slicer(@array1)]; @array2 = @array2[slicer(@array1)]; sub slicer { grep { $_[$_] =~ /keep these/ } 0 .. $#_; }

        -Bryan