Copyright © tutorialspoint.com

PERL grep Function


Syntax

grep EXPR, LIST


Definition and Usage

grep ( EXPR, LIST) extract any elements from LIST for which EXPR is TRUE.

Return Value

  • In scalar context - Number of times the expression returned true

  • In list context - List of elements that matched the expression

Example

Try out following example:

#!/usr/bin/perl

@list = (1,"Test", 0, "foo", 20 );

@has_digit = grep ( /\d/, @list );

print "@has_digit\n";

It produces following result

1 0 20


Copyright © tutorialspoint.com