Updated April 20, 2023
Definition of Perl Grep
In Perl grep function is used to filter out the list of elements form the given input provided in the function as a parameter. Like Linux, it does the same thing of finding the matching vale from the given input. grep is the in-build function available in Perl, inside this function we can pass our regular expression and on the basis of this expression, it will check the matching value in the input and return one or more values as the list depends upon the condition calculated to be true. In the coming section, we will discuss more about this function in detail.
Syntax:
As we know this is the in-build function available in Perl so we do not need to include any library to use this while programming. This function takes two parameters as input. Let’s have a look at its syntax to understand it better see below;
grep(regular_expression, @my_array)
This is syntax given by the Perl doc, as we can see it takes two parameters on the basis of it it print out the result. Let’s discuss one practice example to understand it better for a real scenario see below;
grep("hello", @myarr)
How does Grep Function Work in Perl?
As now we know that grep() function in Perl is used to fitter the array elements. Inside this function, we pass our expression and this expression applied on each and every value of the array, the elements which are evaluated true as per the expression calculation it will return all those elements to us. We can use this function to check whether the element present in the array or not before using that element in our program. But is not good to use grep() function to find an element present in the array, for this purpose we have any() function in Perl available. Let’s discuss the signature of the grep() function in details see below;
Signature
grep(regular_expression, @my_array)
As we can see this function takes 2 parameters as the input, let’s discuss each of the parameter in details see below;
- regular_expression: This parameter is used to pass any expression inside this function. This expression is responsible to calculate or filter out the element from the array which is the given condition. We can write any regular expression inside this, and this regular expression is going to calculate against the array we passed here as the second parameter.
- @my_array: This is the second parameter that is function holds. This array would be the parameter from which we want to filter out our elements based on the expression we pass. This array can be created just like a normal array in Perl language also it can be of any type.
Both of these parameter is needed and mandatory to pass and use the function in order to filter out.
- return type: Before returning any value from the array it first evaluates the value against the expression that we have passed, also all the elements in the array is going to evaluate. If the value satisfies the regular expression then it will be kept as the return value from the array, if the value does not matches the regular expression then it will be discard. This can be a single value or a multiple value that can be returned.
Let’s see one practice example to understand the working of grep() function in Perl in more detail see below;
Example:
# Your code here!
@myarray = ('100', '200', 'Hello', 'Bye');
@result = grep(/^[0-9]*$/, @myarray);
In this example, we are trying to find out only numbers or digit from the string array that we are creating. First, we are creating a string that holds some integer value as well. In the next line, we are using grep() function to identified the integers in the string. So we have created ‘^[0-9]*$’ this as a regular expression that will accept only numbers or digit. So in output, it will not print ‘hello’ and ‘bye’. This is a basic example for beginners to understand how to use this function while programming.
Points to remember while using this function see below;
- This function is used to filter or search the values in the given array.
- we do not need any library to use this function in programming.
- Both input parameters are mandatory to pass otherwise error will be thrown.
Examples of Perl Grep
Following are the examples are given below:
Example #1
In this example we are trying to get only numbers or digit from the string. It will filter only numbers from the array.
Code:
# Your code here!
#creating array to filter element
#also passing expression
@myarray = ('100', '200', 'Hello', 'Bye', '400', '500', 'end of string here !!');
#calling grep method to get the result
@result = grep(/^[0-9]*$/, @myarray);
#print the result
print "Result of the grep function is :::: \n";
print "******************* START ********************** \n";
print @result;
print "\n******************* END *********************** \n";
Output:
Example #2
In this example, we are trying to get only alphabetfrom the array. It will filter the only the alphabet from the array.
Code:
# Your code here!
#creating array to filter element
#also passing expression
@myarray = ('100', '200', 'Hello', 'Bye', '400', '500', 'end of string here !!');
#calling grep method to get the result
@result = grep(/[A-Za-z]/, @myarray);
#print the result
print "Result of the grep function is :::: \n";
print "******************* START ********************** \n";
print @result;
print "\n******************* END *********************** \n";
Output:
Example #3
In this example, we are finding an exact match from the string.
Code:
# Your code here!
#creating array to filter element
#also passing expression
@myarray = ('100', '200', 'Hello', 'Bye', '400', '500', 'end of string here !!', 'matchingParamFound');
#calling grep method to get the result
@result = grep(/matchingParamFound$/, @myarray);
#print the result
print "Result of the grep function is :::: \n";
print "******************* START ********************** \n";
print @result;
print "\n******************* END *********************** \n";
Output:
Conclusion
Grep function in Perl is used to find the given value in the array, both the parameters are necessary to pass. Also using this function, we can check whether the value is present in the given array or not, but this is not recommended to use for this purpose. The basic purpose of this function is to search and filter out the matching values.
Recommended Articles
This is a guide to Perl Grep. Here we also discuss the definition and how does grep function work in perl? along with different examples and its code implementation. You may also have a look at the following articles to learn more –