http://qs321.pair.com?node_id=1056683


in reply to Re^2: perl basic count days between two dates
in thread perl basic count days between two dates

Ok, this code:

my$fullYear; if ( NumberOfDaysBetween($startDay,$endDay ,$endDay,$eMon) == 365){ $fullYear=365; } return $fullYear; }

What happens when NumberOfDaysBetween() is not 365? $fullYear is undefined. Since you have warnings enabled you should see a corresponding warning message. It will still evaluate to 0, but initializing the variable "my $fullYear=0;" is better.

Also, why call NumberOfDaysBetween() with $endDay twice as parameter? Shouldn't there be a $sMon instead?

But if you want to program yourself you surely would prefer finding the bugs yourself instead of us just telling you what to do. So here is how:

It is very fortunate that you are already using subroutines. You first want to make sure that your subroutines are correct. Before you can check what is wrong with the subroutine FullYear you have to be sure that NumberOfDaysBetween really does what you expect it to do. So lets do that:

After the line "use warnings;" at the beginning of your program add the following lines:

print "1,1,1,1 == ",NumberOfDaysBetween(1,1,1,1),"\n"; print "1,1,5,1 == ",NumberOfDaysBetween(1,1,5,1),"\n"; print "1,1,1,2 == ",NumberOfDaysBetween(1,1,1,2),"\n"; print "8,3,3,8 == ",NumberOfDaysBetween(8,3,3,8),"\n"; exit(0);

Now call your script. Is the output as you expect it? If yes, lets be sure and put in some other values and check these too. If you are finally convinced that NumberOfDaysBetween is correct, then do the same with isFullYear(). And so on, until there is nothing left to test

But what happens when you find unexpected results? Then you should add print-statements in that subroutine where you print out the values of variables. Or simply check which path the program takes in an if-clause. For example:

print "daysBetween=",$daysBetween,"\n";

directly after that variable was set would tell you if the program executed the line before and what the result was. Start the program and check if the output you are getting is as you expected. Put in as many print statements as you need, the only downside is that you have to delete them again after you found the bugs

That is everything you need to know at the moment. Should you later have more complex data structures like arrays and hashes I recommend you take a look at the module Data::Dumper to print out those. Also you should know that there is a debugger built into perl. Instead of putting in all those print statements you can step through the program line by line and print out variables while the programm is running without having to change one line of code. But for the moment print-statements will do.

Replies are listed 'Best First'.
Re^4: perl basic count days between two dates
by scripter87 (Novice) on Oct 03, 2013 at 12:34 UTC

    ok using the debug method you have shown my numberofdays sub seem to be out by 1. e.g print "1,1,1,1 == " = O- which is ok. print "1,1,5,1 == " = 0 because the format of the sub is d,cd,m,cm: so when I changed it to 1,1,1,5 it printed 121- which is ok. Now here is where it got weird. "1,1,1,2 == 32 (should be 31) again print "8,3,3,8 == 149 (should be 148). How is it possible to be half correct??

      This is where you should follow the flow of the subroutine with those print statements. Follow along with the math and see where your answer differs from what it should be. Look particularly closely at points where something new changes (such as the first/last day of a month)

      What SuicideJunkie said. Either the logic in NumberOfDaysBetween is wrong or you have to test the subroutines in there (NumberOfDaysInMonth and cumulativeDaysInMonths) similar to NumberOfDaysBetween. If one or both of them produce wrong results then plaster them with print statements until you know what is going on.

      PS: Looked at your code again and subroutines are difficult to see because there isn't even one empty line separating them. Programs are easier to read for humans when structure can be seen at a glance. I even tend to separate subs with lines like "#----------------------" additionally to a few empty lines.

Re^4: perl basic count days between two dates
by scripter87 (Novice) on Oct 03, 2013 at 12:02 UTC

    thank you very much for this debugging help, I will apply this and see how I get on, and yes that was a silly error in that sub.