Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Am I missing something here?

by suggus (Sexton)
on Nov 07, 2001 at 06:48 UTC ( #123759=perlquestion: print w/replies, xml ) Need Help??

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

Ok Monks, Here's some background info before I ask for enlightenment.

I think I'm almost there, but I'm still having problems with the comparing dates...I've decided to go with the Date::Calc module specifically the Delta_Days function. I think I have all the code down but it's not comparing the Delta_Days is not working with my if statement. Can anyone tell me what I'm doing wrong? My guess is my loops are all *@!$ed up but I don't know where to start.

Here's my code:
#!/usr/local/bin/perl5 -wT print "Content-type: text/html\n\n"; use Date::Calc qw(Delta_Days); @Months = ('1','2','3','4','5','6','7','8','9','10','11','12'); ($Month_Day,$Month,$Year) = (localtime)[3,4,5]; $Year += 1900; open(HANDLE,"../script_integration_data/script_integration_records"); while (<HANDLE>) { @temp1 = split(/:/); $domain = unpack ("A4", $temp1[40]); $day_diff = Delta_Days($temp1[28], $temp1[30], $temp1[29], $Year, $ +Month, $Month_Day); if (($domain eq "BBA") && ($temp1[36] eq "In Production") && ($day_ +diff <= 90)) { $sum += $temp1[38]; $count++; } } close(HANDLE); $average = $sum / $count; if ($average >= 44) { print sprintf "<blink><font color=\"#FF0000\">%.f days</font></blin +k>", $average; } elsif (($average >= 11) && ($average <= 44)) { print sprintf "<font color=\"#FFFF00\">%.f days</font>", $averag +e; } elsif ($average <= 10) { print sprintf "<font color=\"#669900\">%.f days</font>", $average; } # EOF
Much Thanks!

-Gus

Replies are listed 'Best First'.
Re: Am I missing something here?
by dws (Chancellor) on Nov 07, 2001 at 07:02 UTC
    "Debugging 101" suggests that a few well-placed print statements might be in order. Before the loop, open a <pre> block. Then, within, the loop, print a few key values, such as the raw date values and $day_diff. That should help you narrow down the problem.

    And are you really sure that it's a date difference problem? The following looks suspect to me:

    $domain = unpack ("A4", $temp1[40]); ... if (($domain eq "BBA") && ...
    You're extracting a 4 character string, then comparing it to a 3 character string. Is that what you really mean to do?

      With regard to the debugging print statements, here's something I do that works very well for me in situations like this. First, at the top of the file, set a debug flag:
      $debug = 1;
      Then I make a debug subroutine:
      sub Debug { if ( $debug ) { my $msg = shift; print $msg, "\n"; } }
      Then throughout the code, I drop in things like:
      Debug( "Entering calculation phase." ); Debug( "Pre-calculation value of \$x: $x" ); ... Debug( "Leaving calculation phase." ); Debug( "Post-calculation value of \$x: $x" );
      This allows me to check values of just about anything, at any point in execution. Then when you're done debugging, you just set $debug to zero and you're done. Personally, I usually leave the debug code in place, in case I ever need to come back and fiddle with the program and need to debug it again.

      HTH
      ___________________
      Kurt

        This will allways call the Debug() sub. The overhead may or may not be a problem in your production systems, it all depends on how many times this call is executed.

        Imagine a few thousand times per minute. (Or per second...)

        I propose the alternative solution:

        $debug=1; $debug&&print("Debug info...\n");

        The above should require less overhead. I've even read somewhere that Perl optimizes this so that the $debug&& doesn't have to evaluated, but unfortunately I can't find back the reference to that text. :-( Maybe some of the well educated Monks may chime in about that?

        If there is no reason to change the value of $debug during execution, the following should be even smarter(?):
        *DEBUG=\1; $DEBUG&&print("Debug info...\n");


        f--k the world!!!!
        /dev/world has reached maximal mount count, check forced.

Re: Am I missing something here?
by Fastolfe (Vicar) on Nov 07, 2001 at 07:17 UTC
    A "use strict;" at the top of that source file couldn't hurt either. Remember also that $Month will be a number from 0 to 11, not 1 to 12.
Re: Am I missing something here?
by DaWolf (Curate) on Nov 07, 2001 at 19:32 UTC
    Well, looking in the background info you provided I saw that you define the month as $temp1[30].

    Wouldn't be easyier to do something like:
    $month = $temp1[30]; $limit = $month + 3; # 90 days = 3 months if ($month >= $limit) { print "Time's up!"; } else { print "You still have some time, enjoy it!"; }
    The solution seems that simple for me, unless I'm missing something...

    Hope that helps.

    Regards,

    Er Galv„o Abbott
    a.k.a. Lobo, DaWolf
    Webdeveloper
Re: Am I missing something here? I GOT IT EVERYONE!
by suggus (Sexton) on Nov 08, 2001 at 04:18 UTC
    Thank you so much to everyone that contributed ideas to my question. Your input has been very valuable! Here's my solution (it's not the tightest code, but it gets the job done)

    #!/usr/local/bin/perl5 -wT print "Content-type: text/html\n\n"; $debug = 0; # Set 1 to enable debug; set to 0 to disable. use Date::Calc qw( Date_to_Days ); @Months = ('1','2','3','4','5','6','7','8','9','10','11','12'); ($Month_Day,$Month,$Year) = (localtime)[3,4,5]; $Year += 1900; $date = Date_to_Days($Year,$Month,$Month_Day); Debug (" Current date is: $date <br>"); open(HANDLE,"../script_integration_data/script_integration_records"); while (<HANDLE>) { @temp1 = split(/:/); $domain = unpack ("A4", $temp1[40]); if (($domain eq "BBA") && ($temp1[36] eq "In Production") && ($temp +1[38] != "-") && ($temp1[38] != "--")) { $script_date = Date_to_Days($temp1[31],$temp1[30],$temp1[29]); Debug ("$temp1[31],$temp1[30],$temp1[29]"); $diff_date = abs ($date - $script_date); Debug ("script date is: $script_date and difference is: $diff_d +ate days. and $temp1[0]<br>"); # If diff_date is greater than 90 days we exectue. if (($diff_date <= 90) && ($diff_date > -$diff_date)) { $sum += $temp1[38]; $count++; } } } close(HANDLE); $average = $sum / $count; if ($average >= 44) { print sprintf "<blink><font color=\"#FF0000\">%.f days</font></blin +k>", $average; } elsif (($average >= 11) && ($average <= 44)) { print sprintf "<font color=\"#FFFF00\">%.f days</font>", $averag +e; } elsif ($average <= 10) { print sprintf "<font color=\"#669900\">%.f days</font>", $average; } sub Debug { if ( $debug ) { my $msg = shift; print $msg, "\n"; } } # EOF


    -Gus I just changed up a bit by taking the absolute value instead of just getting the difference...I almost missed that, but luckily I caught it in time. Time to brush up on that 'ole math. :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://123759]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2023-01-31 00:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?