my $x=1; foreach(@arr){print $x++ if (/foo/)} #### my $str=join ' ',@arr; #### $str=~ /foo/; #### print $&; #### In place of $&; we can use that for no #### for no. of matches.