The grep function in Perl is mainly used for Pattern matching as in other scripting languages.
The “grep” function works on a list. It evaluates an expression or a block for each element of the List. For each statement that returns true as a result of evaluating an expression, it adds that element to the list of returning values.
Look at the following code snippet:
@list = (“foo”,10,0,”bar”,20);
@has_string = grep( /s/,@list );
Output: foo bar
The above code executes the “grep” command on a list and matches the pattern string (/s) to the list. The output is only the elements that are a string.
Posted Date:- 2021-09-05 20:28:17