0

Script given below cuts input string at every occurrence of "E" and store it in a array. Final output shows all the fragmented strings for every position of "E". But the problem is, this script do not show any "E" in the final element of array in the output, it shows only upto "D". Output Which I'm getting is-

ABCDEABCDE ABCDE ABCDE ABCD
ABCDE ABCDEABCDE ABCDE ABCD
ABCDE ABCDE ABCDEABCDE ABCD
ABCDE ABCDE ABCDE ABCDEABCD
ABCDE ABCDE ABCDE ABCDE ABCD

But final output which I want is-

ABCDEABCDE ABCDE ABCDE ABCDE
ABCDE ABCDEABCDE ABCDE ABCDE
ABCDE ABCDE ABCDEABCDE ABCDE
ABCDE ABCDE ABCDE ABCDEABCDE
ABCDE ABCDE ABCDE ABCDE ABCDE

My script is-

my $s = 'ABCDEABCDEABCDEABCDEABCDE';
if (substr($s,-1) eq "E") {
    @array1 = @array[0 .. $#array-1];
    print "Results of 1-missed cleavage having 1E at the end\n\n";
    for my $array1 (@array1) {
        substr($s, $array1-1, 1) = "\0";
        my @a = split(/E(?!P)/, $s);
        substr($s, $array1-1, 1) = 'E';
        $_ =~ s/\0/E/g foreach (@a);
        $result = join ("E,", @a); 
        @final = split(/,/, $result);
        print "@final\n";
    }
    my @output=split(/E(?!P)/, $s);
    $out = join ("E,", @output); 
    @output1 = split(/,/, $out);
    print "@output1\n\n";
}
else {
    print "E is not at terminal position"
};
prashant
  • 97
  • 1
  • 1
  • 8
  • 1
    You should see [my answer](http://stackoverflow.com/a/12372411/133939) to your previous question for a much cleaner way to do this. – Zaid Sep 12 '12 at 08:38

1 Answers1

1

Considered going a bit simpler?

my $s = 'ABCDEABCDEABCDEABCDEABCDE';

# Split on "E not followed by whitespace".
# Replace with "E<space><matched char, if any>"
$s =~ s/E([^\s]?)/E \1/g;

print $s, "\n\n";

Output:

haiku:~$ perl blah.pl 
ABCDE ABCDE ABCDE ABCDE ABCDE 

haiku:~$

Perl is an extremely powerful text manipulation tool, and it's regular expressions are second to none (IMHO).

Spikes
  • 1,036
  • 1
  • 6
  • 8
  • Thank you for your response. But first do consider the pattern of first 4 and last output line, then suggest possible solution. – prashant Sep 12 '12 at 08:27
  • 3
    As it were, your question seems like a homework question. I'm having a hard time envisioning this sort of scenario in any real world code. (While possible, it's not striking me as likely.) So, rather to answer your question out-right, in the event that it is a homework question, I gave you a hint towards your answer, rather than give you the most direct solution. – Spikes Sep 12 '12 at 08:29