Friday, July 29, 2011

finding the no of values in array match a pattern

grep can also be used to find no of values in a array match the pattern specified by assigning to the scalar.
using the below example:


@a=('one','Two','Three','Four','Five');
$ff=grep(/^T/,@a);
print "$ff";

Output:

G:\perl_programs>perl perl_grep.pl
2
G:\perl_programs>

Explanation:
Here the pattern searched through arrays is "^T".So here Two and Three values match Since it is assigned to a scalar it is converted to 2(no of values that match)

No comments:

Post a Comment