foreach my $thing (@lines) { if ($thing =~ /<% INCLUDE exclusives(\d+)\.tmpl %>/) { my $num = $1 == 1 ? 3 : 1; $thing =~ s/<% INCLUDE exclusives(\d+)\.tmpl %>/<% INCLUDE exclusives$num\.tmpl %>/; } }