use strict; use warnings; my @a=(2,4,7,5,8); my ($position, $spaces); # Elements have not " " ("@a" concatenate with spaces) if ("@a" =~ /(?:^|\ ) # Element searched maybe the first or not \K # Count previous group matched within ${^PREMATCH} var (?{ $position=pos; $spaces= () = ${^PREMATCH} =~ m, ,g; }) # Next group will be that which are we searching, so # we take note about where is it and how many spaces # should we ignore ("@a" add spaces) (8) # Element searched (?:\ |$) # Element searched maybe the last or not /xpg) { print "Position:" ,$position - $spaces, "\n"; }