foreach (@arr) { $str =~ s/($_)/random{$1}/g; #where random is my map}