Well the following replaces the front of the strings as per your spec, I couldn't be arsed to do the end of the strings, as you need to know in advance where the next string starts (and if its before the endpoint of your current string). If might be best to search first and save the start/end positions of the substrings first and then go through replacing values.
This should be a good start though.
#!/perl -w
use strict;
my $string = "abcdefghijklmnopqrstuvwxyz";
my @sub_strings = ('cdef', 'efghij', 'klmno', 'mnopqrst');
my $string_index = 0;
my $last_end_point = 0;
foreach my $sub_string (@sub_strings) {
my $string_index = index($string, $sub_string, $string_index);
die ("Couldn't find string") if $string_index == -1;
my $overlap = $last_end_point - $string_index;
if ($overlap > 0) {
substr($sub_string, 0, $overlap) = '-'x$overlap;
}
$last_end_point = $string_index + (length($sub_string));
}
print join(',', @sub_strings);
update: removed stupid variable declaration
---
my name's not Keith, and I'm not reasonable.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.