Reputation:
I have this PERL Switch..case statement:
switch ($today)
{
case "14-Aug-2012" { do A }
case "15-Aug-2012" { do B }
}#end switch
My problem is that the "do B" statement is the same for 15-Aug-2012 to like 01-Oct-2012. How do I say case between these dates so I don't have to rewrite the same thing for different days and making my script long?
I've put in the whole script so someone could help me use what I have to manage my question.
use Date::Calc
use Switch
#connect to database...
my @date_today=Today();
my $today=substr(Date_to_Text(@date_today),4,11);
Switch($today)
{
case "14-Aug-2012" {Do A}
case "15-Aug-2012" {Do B}
case ...
case ...
case ...
}
The last 3 case statements are supposed to do:
between 16-Aug-2012 and 28-Sep-2012 {do C}
between 29-Sep-2012 and 26-Oct-2012 {do D}
between 27-Oct-2012 and 09-Nov-2012 {do E}
Upvotes: 0
Views: 2799
Reputation: 6566
Here is another way of doing it that may be a bit simpler. It simply converts the dates into YYYYMMDD format, which allows them to be sorted/compared numerically.
sub sortable_date
{
my %months;
@months{
'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'
} = ('01'..'12');
if (shift() =~ /^(\d{1,2})-(\w{3})-(\d{4})/ and exists $months{$2})
{
return "$3$months{$2}$1";
}
else { die "invalid date format!"; }
}
switch (sortable_date($today))
{
case sortable_date("14-Aug-2012") { do A }
case ($_[0] >= sortable_date("15-Aug-2012") and
$_[0] <= sortable_date("01-Oct-2012")) { do B }
}
I would recommend mpe's approach if you are going to be doing a lot with dates in general, though.
Upvotes: 0
Reputation: 1000
Use UNIX timestamps instead of date strings. Timestamps are integers and can easily be classified into date ranges (and reformatted using localtime()
).
For example, use Time::Local
and its timelocal()
function to turn your string into a timestamp:
use Time::Local;
my %months = ( # necessary because timelocal() expects month numbers, not strings
'Jan' => 0,
'Feb' => 1,
'Mar' => 2,
'Apr' => 3,
'May' => 4,
'Jun' => 5,
'Jul' => 6,
'Aug' => 7,
'Sep' => 8,
'Oct' => 9,
'Nov' => 10,
'Dec' => 11
);
my $today = '15-Aug-2012';
my @t = $today =~ /(\d{4})-(\w{3})-(\d{4})/;
$t[1] = $months{$t[1]}; # turn string into integer
my $timestamp = timelocal(0, 0, 0, @t[0, 1, 2]); # sec, min, hr, day, month, year
Upvotes: 0
Reputation: 129481
Use a software engineering approach.
If you need to do the same thing for an entire range of days, use that range's ID as an discreet value to choose on. Then have a subroutine to tell you what the range ID is that the date falls on:
sub range_for_date {
my $date = shift;
# Compute the range value in some discreet subset
# How to compute it is somewhat irrelevant
# and can be asked separately if you have no idea
# discreet subset can be an index 1..N, ecpoch timestamps,
# or a somehow-encoded range end date (e.g. "20121001" is easy)
# For the switch example below we will assume end dates
return $range_id; # in "20121001" format
}
switch (range_for_date($today)) {
case "20121001" { do B; }
case "20120110" { do A; }
}
Upvotes: 2