vivomancer has asked for the wisdom of the Perl Monks concerning the following question:

This is my current working code. Its purpose is to find a string of amino acids that match a regular expression and put the matching string as well as what is found before and after the matching string in 3 separate scalars.
if($sequence[1] =~ m/^(.*)$reg(.*)$/i){ $aligned[$posCount][0] = $1; $aligned[$posCount][1] = $2; $aligned[$posCount][2] = $3; }
$reg used to be a regular expression contained in a single set of parens. Example:  $reg = (K[STN]\w\w[GST]\w{1,2}[KRA]R[IVF]); But now I have set it up to automatically place parens around \w of variable length, so the previous $reg would now be:  $reg = (K[STN]\w\w[GST])(\w{1,2})([KRA]R[IVF]); Such that a string searched against a regular expression with 1 variable length component would be split into 5 pieces. The stuff found before the match, constant length match1, variable length match, constant length match2, the stuff found after the match. I want to make it so a user could have any number of variable length regions. Is there a method with which I could add recursion?
if($sequence[1] =~ m/^(.*)$reg(.*)$/i){ for(my $x = 0; $x < $numInsertionDeletes; $x++){ $aligned[$posCount][$x] = ????????; } }

Replies are listed 'Best First'.
Re: Need a Method to add recursion to my regex
by tobyink (Canon) on Jun 18, 2012 at 19:28 UTC

    I think what you're looking for is:

    $aligned[$posCount] = [ $sequence[1] =~ m/^(.*)$reg(.*)$/i ];

    If you provided some sample input and expected output, then we could know for sure.

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
      I think this is exactly what I needed. So far all testing that I've done has yielded the expected results. Thank you.
Re: Need a Method to add recursion to my regex
by choroba (Cardinal) on Jun 18, 2012 at 19:28 UTC
    Untested:
    if (my @matches = $sequence[1] =~ m/^(.*)$reg(.*)$/i) { @{ $aligned[$posCount] }[ 0 .. $numInsertionDeletes - 1 ] = @match +es; }