in reply to Yet More fun With Array Indices

If you're just removing items that match/don't match a pattern, grep is the easiest choice:
my @array = qw/1 2 3 4 5 6 7 8 9 10 11 12/; my $pattern = qr/2/; @array = grep { ! /$pattern/ } @array; # @array now contains qw/1 3 4 5 6 7 8 9 10 11/

blokhead

Replies are listed 'Best First'.
Re^2: Yet More fun With Array Indices
by Aristotle (Chancellor) on Sep 14, 2002 at 17:01 UTC
    Bullseye - I don't understand why everyone else got stuck on the for loop. Some minor points though: if you're using patterns for matches, you should consider using anchors: @array = grep { ! /^$pattern$/ } @array; In this case it's better to just use a comparison: @array = grep $_ ne $whatever, @array;

    Makeshifts last the longest.

Re: Re: Yet More fun With Array Indices
by graff (Chancellor) on Sep 15, 2002 at 03:34 UTC
    Since Elgon's original code appeared to be doing numeric tests, it's worth pointing out that grep will work with numeric operations as well as string matches. For instance:
    @a=(1,2,3,4,5); @b=grep {int($_/2) == 1} @a; print join $/,@b,"";
    prints out the values 2 and 3.
Re: Re: Yet More fun With Array Indices
by Elgon (Curate) on Sep 14, 2002 at 18:51 UTC

    Once again my faith in Perlmonks is vindicated - Deliciously lazy, redolent with hubris and positively teeming with impatience. Cracking solution.

    Thanks, Elgon.

    "Rule #17 of Travel: Never try and score dope off Hassidic Jews while under the impression that they are Rastafarians."
           - Pete McCarthy, McCarthy's Bar