Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I do a reverse grep using Perl?

Tags:

grep

perl

What is the equivalent of grep -v in Perl?

The code looks like this

@files = reversegrep(/^[ ]+$/, @files);

I want @files to have a list of all the file names that are not empty.

like image 768
Lazer Avatar asked Nov 17 '10 09:11

Lazer


People also ask

How do I grep a string from an array in Perl?

The Perl grep() function is a filter that runs a regular expression on each element of an array and returns only the elements that evaluate as true. Using regular expressions can be extremely powerful and complex. The grep() functions uses the syntax @List = grep(Expression, @array).


1 Answers

You can use negation in the expression or block:

@files = grep !/^\s+$/, @files;

See perldoc grep.

like image 64
Eugene Yarmash Avatar answered Nov 10 '22 10:11

Eugene Yarmash