in reply to How do you perform this substitution?

NOT because this is useful, elegant, or even extensible code: hardcoded vars, sted calculated vars; RITW (and in this case, probably a five-wheeled q'cycle). But just because embedded newlines intrigue me...

#!\usr/bin/perl use warnings; use strict; use 5.016; # 1091171 with modifications to handle strings w/embedded newlines my @data = <DATA>; my $source = ('-' x 50 . "\n" . '-' x 75); my @id = split /\n/, $source; my ($tag, $line, $id, $offset, $end, $substr_len, $len, $range); for $id(@id) { chomp $id; if ( $id =~ /^-{50}$/ ) { $len = length($id); $tag = "VAR1: "; for $range(@data) { ($offset, $end) = split /-/, $range; chomp $offset; chomp $end; my $substr_len = ($end - $offset + 1); if ($offset + $substr_len <= $len) { substr ($id[0], $offset - 1, $substr_len) = ('A' x + $substr_len); say "$tag: id[0]\n\tModified line: |$id[0]|\n"; } else { say "At Ln26: \$end: $end > \$len: $len"; say "Replacements attempted beyond end of string.\ +n\n"; next; } } $id = ''; } else { $tag = "VAR2: "; say "\n\t\t NOW WORKING ON $tag \n"; chomp $id[1]; my $len = length($id[1]); for $range(@data) { my ($offset, $end) = split /-/, $range; chomp $offset; chomp $end; my $substr_len = ($end - $offset + 1); if ($offset + $substr_len <= $len) { substr ($id[1], $offset - 1, $substr_len) = ('B' x + $substr_len); say "$tag: id[1]\n\tModified line: |$id[1]|\n"; } else { say "At Ln55: \$end: $end > \$len: $len"; say "Replacements attempted beyond end of string." +; } } } } =head OUTPUT C:\>1091171.pl VAR1: : id[0] Modified line: |------AAAA------------------------------------ +----| VAR1: : id[0] Modified line: |------AAAA-AAAAAAAAAAAAAAAAAAA---------------- +----| At Ln26: $end: 60 > $len: 50 Replacements attempted beyond end of string. NOW WORKING ON VAR2: VAR2: : id[1] Modified line: |------BBBB------------------------------------ +-----------------------------| VAR2: : id[1] Modified line: |------BBBB-BBBBBBBBBBBBBBBBBBB---------------- +-----------------------------| VAR2: : id[1] Modified line: |------BBBB-BBBBBBBBBBBBBBBBBBB----BBBBBBBBBBBB +BBBBBBBBBBBBBB---------------| C:\> =cut __DATA__ 7-10 12-30 35-60

Come, let us reason together: Spirit of the Monastery

... but check Ln42!