Grep - removing multiple array elements
- I am having a problem with "grep". I want to remove certain known
elements from an array of strings - the code below is an example:
my @line = ("p1", " ", "Yes", "p2", "No", "Help!", "N/A");
my @remove = ("Yes", "No", "N/A");
@line = grep !/[@remove]/, @line;
I would have expected the output to be "p1 p2Help" (note the
whitespace), however what I am actually getting as output is "p1p2".
I can't figure out how to get what I want, and I am confused why I
get what I am getting.
Any help is appreciated.
- hooyar66 wrote:
: @line = grep !/[@remove]/, @line;
Character classes match single characters, not words.
use Data::Dumper 'Dumper';
my @lines = ('p1', ' ', 'Yes', 'p2', 'No', 'Help!', 'N/A');
my @remove = ('Yes', 'No', 'N/A');
my $regex = join '|', @remove;
@lines = grep !/$regex/, @lines;
print Dumper \@lines;
Charles K. Clarkson
Mobile Homes Specialist
Free Market Advocate
Don't tread on my bandwidth. Trim your posts.