Suppose I have a string, and an array as follow:
my $str = "currentStringwithKey<i>";
my @arr = ("1", "2");
So, is there any better way to quickly replace the string with every value in the array, rather than using for loop, and put the output of each replacement into new array.
My expect output was:
my @outputArray = ("currentStringwithKey1", "currentStringwithKey2");
Without using for loop use map for to do it
/r is the non-destructive modifier used to Return substitution and leave the original string untouched
my $str = "currentStringwithKey<i>";
my @arr = ("1", "2");
my @output = map{ $str=~s/<i>/$_/rg } @arr;
#$str not to be changed because of the r modifier
print @output;
Then the @output
array contain like this
$output[0] = "currentStringwithKey1",
$output[1] = "currentStringwithKey2"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With