Perl Grep: How to Use this Filtering Function

perl grepThe Perl programming language is loosely written, and it doesn’t have as many restrictions as other languages. Because of this, you can create lists with varying elements. There are lists in Perl that consists of both strings, integers, and other symbols. Because of this freedom, Perl grants you the ability to do a lot more with your programs without having to worry about keeping everything the same type.

This also makes the Perl grep function incredibly useful. With the Perl grep function, you can filter out elements from your list, and even create a sublist for it. This allows you to play with specific elements of your program while keeping everything else unchanged.

Check out the Udemy course, Beginning Perl, if you’re curious about using Perl’s grep functions and other utilities. Perl is a great programming language to use if you’ve never done programming before.

Although if you really want to learn the very basics of programming, which is incredibly useful, when you’re trying to code, then you can check out another Udemy course, Programming for Non-Programmers. 

What is a Grep Function? 

To put it simply, the Perl grep function allows you to filter a list. You can then return the elements you filtered from your list that matches a specific condition. Usually the list will be shorter than the original list.

Imagine creating a list that involved all of the people in a school, which includes the students, faculty, staff, and even parents who volunteer their time. You compose a list of all of these people in Perl, but you only want to send something to the students. In order to do this, you will need to filter out all of the students from the list and then use the elements from that filter. This is where the Perl grep function comes in; through this function you can extract all of the students from the list in a simple manner. This can make things a lot easier, when you have dozens or even hundreds of elements in a list at a time.

How to Create a Grep Function?

Now that you understand the purpose of a grep function, let’s look at how to create one. A grep function has three different parameters to it. There are two ways to write a grep function.

grep (EXPR, LIST)

In the first function, BLOCK means that the function has at least one statement that is delimitated by braces. Whatever the last statement of the block is will determine if the entire block is true or false. In addition to this, the block will be evaluated for every element that of that’s in the list. If the result is true for that element, then the element will be added to the list that is returned. Using the Perl grep function’s block can be helpful, when you’re in need of a filter that has more than one line of code.

The EXPR or expression parameter represents any expression. In Perl, expressions are represented by $_ and they are applied to every element in the list. If the results are true, then the element will be added to the list.

The final parameter is LIST, and this is simply the list of elements that you will find in your array. 

How Does the Grep Function Work?

The grep function works by iterating through every element of your list. In every element, the function will set up your expression $_, and then it will evaluate it to either the block or EXPR part of your function. Whenever the result is true, that element is added to the output list. There is also the possibility that it can just increment the number of times the element matches.

For instance, you could use a grep function to count all of the students in your list. Even though it is a list of 300 people, the function will only count the 200 students. This means that the output for the function would be 200.

Examples of a Grep Function 

Now that you’ve seen how grep functions work, you can see some examples and see how they are written and used.

my @thearray = qw(2 3 4 5 6 7 8 9 10);
my @newarray = grep {$_ & 2} @thearray;

Above is a simple example of how you would use a grep function. The first line creates an array with numbers in it. The second array creates a grep to go through the elements in the @thearray. Now what happens is that the grep function will take each element and evaluate the condition $_ & 2, which will essentially check and see if the numbers are even. When it finds that the numbers are even, the grep function will add it to the @newarray. If you were to print out the new array after the grep function was done, it would read out the following numbers.

2 4 6 8 10

Perl can be very useful when you’re trying to quickly create sublist, but it isn’t the only way. You can do the same thing shown above with a foreach loop. There are various functions in Perl that you can learn about. There are even specific Perl string functions that are designed to help you manipulate your code easily.

Learning More about Programming

You can explore the various benefits of programming with Perl, and a lot of other languages. The greatest thing about programming is that, once you get the hang of one language, you can pick up on other ones pretty easily. One language that a lot of beginners start learning is Python. Not only is Python versatile, but it’s also not as strict as most other languages.

If you really want to see how you can use Python and how it compares to Perl, then check out the Udemy courses Learn Programming with Python and Programming for Absolute Beginners. Not only will both courses teach you about a useful language, but they will also help you with mastering the fundamentals of programming.