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!


In reply to Re: How do you perform this substitution? by ww
in thread How do you perform this substitution? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.