my @ranges = qw(1..4 5..8 9..12 13..16 1..8 9..16 ) foreach my $range ( @ranges ) { foreach($range){ substr($str1,$_,1) = '1'; } }