my @vars = split ...; $template =~ s/ \[ VAR (\d+) \] /$vars[$1]/gx;