[Solved] split string into several substring using indexes


You can use substr() as an lvalue and start replacing the string from right side of it,

my $string = 'GATGCAGTTAGGCGTAGCAGAGTGAGACGACGACGATATTAGGACCCGGTAAGGCACAATATAGC';
my %coord_colors = (
  10 => "red",
  48 => "orange",
  60 => "purple",
);

substr($string,$_,0) = $coord_colors{$_}
  for sort { $b <=> $a } keys %coord_colors;

print $string;

output

GATGCAGTTAredGGCGTAGCAGAGTGAGACGACGACGATATTAGGACCCGorangeGTAAGGCACAATpurpleATAGC

using regex,

$string =~ s|.{$_}\K|$coord_colors{$_}|s
  for sort { $b <=> $a } keys %coord_colors;

solved split string into several substring using indexes