my $source_pos = 0; my $end_string = $str_with_dash; for (my $i = 0; $i < length $str_with_dash; $i++) { next if substr($str_with_dash, $i, 1) eq '-'; substr($end_string, $i, 1) = substr($str_no_dash, $source_pos, 1); $source_pos++; }