Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

if && condition not working

by rakheek (Sexton)
on Mar 24, 2010 at 18:56 UTC ( [id://830638]=perlquestion: print w/replies, xml ) Need Help??

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

Wise Monks, I have a simple if && condition which is not working for me. I am perplexed. Please help. The code is below: Regards, Rakhee
sub validate_date { my $start_date = $_[0]; my $end_date = $_[1]; my $today_date = $_[2]; my @start_date_array = split(/\//, $start_date); my @end_date_array = split(/\//, $end_date); my @today_date_array = split(/\//, $today_date); my $error_msg = ""; if (!$start_date || !$end_date){ $error_msg = "Blank Field in Date field. Please Use Browser Ba +ck button and Enter Date Field"}; if ( (@start_date_array[0] > @today_date_array[0]) && (@start_ +date_array[1] > @today_date_array[1]) && (@start_date_array[2] > @tod +ay_date_array[2])) { $error_msg = "Start Date can not be a Future Date. Please Use +Browser Back button and Enter Start Date"}; if ( (@end_date_array[0] > @today_date_array[0]) && (@end_date +_array[1] > @today_date_array[1]) && (@end_date_array[2] > @today_dat +e_array[2])) { $error_msg = "End Date can not be a Future Date. Please Use Br +owser Back button and Enter End Date"}; return $error_msg; }

Replies are listed 'Best First'.
Re: if && condition not working
by ikegami (Patriarch) on Mar 24, 2010 at 19:13 UTC
    Start by not hiding/ignoring warnings
    Scalar value @start_date_array[0] better written as $start_date_array[ +0] at a.pl line 12. Scalar value @today_date_array[0] better written as $today_date_array[ +0] at a.pl line 12. Scalar value @start_date_array[1] better written as $start_date_array[ +1] at a.pl line 12. Scalar value @today_date_array[1] better written as $today_date_array[ +1] at a.pl line 12. Scalar value @start_date_array[2] better written as $start_date_array[ +2] at a.pl line 12. Scalar value @today_date_array[2] better written as $today_date_array[ +2] at a.pl line 12. Scalar value @end_date_array[0] better written as $end_date_array[0] a +t a.pl line 14. Scalar value @today_date_array[0] better written as $today_date_array[ +0] at a.pl line 14. Scalar value @end_date_array[1] better written as $end_date_array[1] a +t a.pl line 14. Scalar value @today_date_array[1] better written as $today_date_array[ +1] at a.pl line 14. Scalar value @end_date_array[2] better written as $end_date_array[2] a +t a.pl line 14. Scalar value @today_date_array[2] better written as $today_date_array[ +2] at a.pl line 14.

    Mind you, that warning doesn't indicate a real problem here (it points out a bad style/habit that can have an effect in uncommon circumstances), but who knows what other warnings are being issued. Add use warnings; if it's not already there.

Re: if && condition not working
by ikegami (Patriarch) on Mar 24, 2010 at 19:20 UTC

    Sounds like the dates are in the yyyy/mm/dd format, so why not just compare the strings with lt/gt?

    sub validate_date { my ($start_date, $end_date, $today) = @_; return "Invalid format for start date (expecting yyyy/mm/dd)" if $start_date !~ m{^\d{4}/\d{2}/\d{2}\z}; return "Invalid format for end date (expecting yyyy/mm/dd)" if $end_date !~ m{^\d{4}/\d{2}/\d{2}\z}; return "End date must be no later than today" if $end_date gt $today; return "Start date must be no later than end date" if $start_date gt $end_date; return ""; }
      Thanks, Ikegami. I will use the string comparison. Do you know if I need to get rid of the / in the date? I can give it a try. Regards, Rakhee
        "/" equals "/", so no need to remove them. gt performs a string comparison, not a numerical one.
Re: if && condition not working
by toolic (Bishop) on Mar 24, 2010 at 19:14 UTC
    Use perltidy to neaten up your code. Maybe you will be less confused.

    use strict and warnings. I get warning messages when I compile your code:

    perl -wc -Mdiagnostics 830638.pl
    You should eliminate the warnings before proceeding.

    Also, consider using one of the many CPAN Date modules. Date::Calc has a "How do I compare two dates?" recipe.

Re: if && condition not working
by Anonymous Monk on Mar 24, 2010 at 18:58 UTC
    Can you show us your data?
      A date in the future can have the same month and year as today. Tomorrow is a good example (assuming today is not the last day of the month or year).

        assuming today is not the last day of the month or year

        He doesn't handle that either, mind you.

      Thanks for replying Anonymous Monk. Yes, my data is right. I printed it out. Also, if I use if condition without the &&, it works. I am totally confused. Here is what worked:
      sub validate_date { my $start_date = $_[0]; my $end_date = $_[1]; my $today_date = $_[2]; my @start_date_array = split(/\//, $start_date); my @end_date_array = split(/\//, $end_date); my @today_date_array = split(/\//, $today_date); my $error_msg = ""; if (!$start_date || !$end_date){ $error_msg = "Blank Field in Date field. Please Use Browser Ba +ck button and Enter Date Field"}; # if ( (@start_date_array[0] > @today_date_array[0]) && (@start_ +date_array[1] > @today_date_array[1]) && (@start_date_array[2] > @tod +ay_date_array[2])) { if (@start_date_array[0] > @today_date_array[0]) { $error_msg = "Start Date can not be a Future Date. Please Use +Browser Back button and Enter Start Date"}; if ( (@end_date_array[0] > @today_date_array[0]) && (@end_date +_array[1] > @today_date_array[1]) && (@end_date_array[2] > @today_dat +e_array[2])) { $error_msg = "End Date can not be a Future Date. Please Use Br +owser Back button and Enter End Date"}; return $error_msg; }
        The request was not if your data is properly formatted. The request was to show us examples, so we know what to feed into the subroutine to test it. How are your dates formatted? Is it month/day/year, day/month/year, year/month/day? Is January represented by "1" or "01"? This information is very significant for showing you how your code is incorrect.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2024-04-19 20:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found