I am trying to break a string at every occurrence of ‘E’. But in output I want to skip those substrings which had arrived earlier in the previous loop of breakage. For eg. If I take a string $s='ABCDEABCDEABCDEABCD' where ‘E’ is present at 5th, 10th and 15th position in a string, substrings which appeared as the result of breakage at position 10 should not appear in the substrings resulting from the breakage at position 15. Script given below breaks at every position of ‘E’. But I’m unable to stop the substring repetitions. Please help!
my $s = 'ABCDEAXBCDEAYBCDEAZBCD';
my @where; my @array;my @final;
my $result; my $j;
for ($j = 0; $j <= 2; $j++) {
$where[j] = index($s,"E",$where[j-1]) + 1;
push @array, $where[j];
}
for my $array (@array) {
substr($s, $array-1, 1) = "\0";
my @a = split(/E(?!P)/, $s);
substr($s, $array-1, 1) = 'E';
$_ =~ s/\0/E/g foreach (@a);
$result = join ("E,", @a).'E';
@final = split(/,/, $result);
print "@final\n";
}
Output which I’m getting is:
ABCDEAXBCDE AYBCDE AZBCDE
ABCDE AXBCDEAYBCDE AZBCDE
ABCDE AXBCDE AYBCDEAZBCDE
Expected Output:
ABCDEAXBCDE AYBCDE AZBCDE
ABCDE AXBCDEAYBCDE
AXBCDE AYBCDEAZBCDE
I don’t understand the purpose/description of your problem, but you can add a hash that keeps track of substrings you have seen previously and drop them, by changing the last loop to something like:
That changes the output to:
Your code looks like it has been translated from C. If you try to describe more clearly what it is you want to do, maybe someone can help your with a more idiomatic Perl version.