in reply to recursive algorithm

You probably think that

@bases = split(//,"5.$basestring.3");

adds only '5' at the beginning and '3' at the end. But you are actually adding '5' and '.' at the beginning and '.' and '3' at the end. Check if your code works with

@bases = split(//,"5".$basestring."3");

or

@bases = split(//,"5${basestring}3");

Replies are listed 'Best First'.
Re^2: recursive algorithm
by Anonymous Monk on Sep 06, 2012 at 12:41 UTC

    This works without a warning

    sub evalRna { my ( $left, $right ) = @_; my %bonds = ( GU => 1, UG => 1, AU => 2, UA => 2, CG => 3, GC = +> 3 ); my $numBonds = $bonds{ $bases[$left] . $bases[$right] }; my $level = 0; my $new_left = $left; my $numRna; for ( my $i = $left + 1 ; $i <= $right ; $i++ ) { $level-- if ( $structure[$i] eq ')' ); if ( $level == 0 ) { if ( $structure[$i] eq ')' ) { if ( $numRna = evalRna( $new_left, $i ) ) { $numBonds += $numRna; } } $new_left = $i; } $level++ if ( $structure[$i] eq '(' ); } return $numBonds; }

    As you can see, I made some changes for better reading experience. The only change needed is to check if evalRna() returns undef. This caused your warnings. Maybe the return value undef is not, what your intention was.