Date: 13 Sep 2001 16:17:31 -0400 From: Joe Schaefer Subject: Re: deleting items from array - better way? Message-Id: "Daniel Berger" writes: > Hi all, > > I want to delete all values from an array that match $val. Note that I > don't want to merely replace them with null strings (defined or not). I dug > around the "one-liners" section of TPJ, the cookbook and this newsgroup but > didn't see one. > > e.g. Get rid of all values that match "one". > my @array = qw(one two three one one); > my $val = 'one'; > # Begin 2 liner > my $n = 0; > map{ if($_ =~ /$val/){ splice(@array,$n,1); $n--; } } @array; ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ I doubt that does what you want- in fact I think it looks like @array = qw/two one/; when you are done. Here's some one-liners that should work: # mostly matching values @array = grep !/$val/, @array; # mostly non-matching values $array[$_] =~ /$val/ and splice @array, $_, 1 for reverse 0..$#array; -- Joe Schaefer "Few things are harder to put up with than the annoyance of a good example." --Mark Twain