Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

How To Debug

by DrHyde (Prior)
on Sep 06, 2012 at 10:23 UTC ( #992055=note: print w/ replies, xml ) Need Help??


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.


Comment on How To Debug
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://992055]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2014-12-18 05:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (42 votes), past polls