http://www.perlmonks.org?node_id=992055


in reply to recursive algorithm

What's line 27? It's hard to tell because of the line wrapping that might have happened around the text "hydrogen bonds in this structure". Also your code is unreadable because you haven't indented it, there's no comments, and your variable names aren't meaningful (having $i and $ii is confusing), so I'm not even going to bother trying to figure it out. So what follows is pretty generic ...

How you debug this is simple: either in the debugger or using print statements, spit out the variables on line 27 and if any of them are not what you expect, then look through your code to find where they are defined (or not defined), and so on, until you find the problem.

If you *are* expecting undef variables, then you'll want to guard any against using them inappropriately. For example, if in this line:

$level--  if ($structure[$i] eq ")");

$structure[$i] may legitimately not be defined, then you should check for definedness before the comparison. Or if it's because $structure[$i] might legitimately not exist (eg if $i is beyond the end of the array) then check for existence instead of (or as well as) definedness, to prevent auto-vivification.

If you were dealing with a hash here instead of an array, I'd just point you at Tie::Hash::Vivify - one of the "interesting ways" it can auto-vivify is by dieing with a helpful message - see this for an example. But I can't see anything on the CPAN to do similar things for arrays.