#! /usr/bin/perl use strict; use warnings; my $x=1; my @arr= qw(foo cho roh foo kho foo moo foo); foreach(@arr){print $x++ if (/foo/)} #### #! /usr/bin/perl use strict; use warnings; my $x=1; my @arr= qw(foo cho roh foo kho foo moo foo); my $str=join ' ',@arr; $str=~ /foo/; print $&; #### In place of $&; we can use that for no #### for no. of matches.