Are you given ABCD or are you given the position and length (or start and end) of the substring? Can the * and !'s occur anywhere? You should give a before and after sample. Something like this:

__DATA__ XXXXXXABCDXXXXXXXX XX**XXX!!!X**AB*!C*DXX*!!!XXX**XXX

Want to print:

10 characters added XXXXXXABCD**!!!***!*XX*!!!XXX**XXX

The following code may be a good starting point:

use strict; use warnings; my $match = 'ABCD'; while (<DATA>) { my $org = $_; defined (my $mutated = <DATA>) or die "Missing edited line"; my $segment = substr $mutated, 0, index ($mutated, substr $match, 3, + 1) + 1; my $suffix = substr $mutated, index ($mutated, substr $match, 3, 1) ++ 1; (my $pInsert = $segment) =~ tr/*!//cd; (my $pSegment = $segment) =~ tr/*!//d; print length ($pInsert) . " characters added\n"; print "$pSegment$pInsert$suffix\n"; } __DATA__ XXXXXXABCDXXXXXXXX XX**XXX!!!X**AB*!C*DXX*!!!XXX**XXX

Note that some error checking should be added and that there are a few assumptions about the match string and what the X sequences can actually contain.


DWIM is Perl's answer to Gödel

In reply to Re: confused with strings by GrandFather
in thread confused with strings by ktsirig

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.