http://www.perlmonks.org?node_id=1043504

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

Hello Holy Ones

I am modifying a script that currently runs through a table of normal business hours, stores that data in a hash, then runs through a table of exceptions to our business hours and stores any found for the current week to a hash, then writes this week's business hours to an html table. The script uses Date::Simple.

The current way this information is formatted is Sunday through Saturday. I need to change it so that the script runs every day, then posts today's business hours, with a drop-down table of the next 6 days' business hours. I think that what I need to do is sort my normal hours according to the time range of today () to today () + 6, but I don't know how to do that (or if I can do that?).

Here is what I believe is the relevant code:

# -- define today my $today = today(); # -- use today object and return a number indicating the current day o +f week (0=sun 1=mon, etc.) my $dow = $today->day_of_week; # -- get the start and end dates for the week # -- Date module helps us do this easily my $today_date = ( $today ); # start of week my $end_date = ( $today_date + 6 ); # end of week # -- will store data for Sun <-> Sat with day of week (number) as key my %this_wk = (); # -- simple arrays to define days my @days = (0 .. 6); my @day_text = qw(Sunday Monday Tuesday Wednesday Thursday Friday Satu +rday); # -- loop thru all days of week and add data to hash # -- we will add more data to hash later for my $day (@days) { # -- add date and day text identifier to hash $this_wk{$day}{date} = ( $today_date + $day ); $this_wk{$day}{day_text} = $day_text[$day]; } ## end days for loop # -- connect to Voyager db my $dbh = DBI->connect( $voy_dsn,$voy_dbun,$voy_dbpass, { AutoCommit = +> 0, RaiseError => 0, PrintError => 1 } ) || die $DBI::errstr; # -- get normal hours of operations # -- yes, believe it or not there is only one row (there must be a rea +son for this setup but I can't think of one) my $sql = qq(SELECT sunday_open,sunday_openhour,sunday_closehour, monday_open,monday_openhour,monday_closehour, tuesday_open,tuesday_openhour,tuesday_closehour, wednesday_open,wednesday_openhour,wednesday_closehour, thursday_open,thursday_openhour,thursday_closehour, friday_open,friday_openhour,friday_closehour, saturday_open,saturday_openhour,saturday_closehour FROM calendar WHERE calendar_desc = '$voy_cal2use'); my $sth = $dbh->prepare( $sql ); $sth->execute; # -- fetch all data and create var to refer to my $arrayref_norm_hrs = $sth->fetchall_arrayref( ); # -- store normal hours for each day of week in here my %normal_hrs = (); # -- loop thru the data array returned by query # -- put in a data structure we can work with foreach my $row ( @$arrayref_norm_hrs ) { my ( $sun_open,$sun_openhr,$sun_closehr,$mon_open,$mon_openhr,$mon_cl +osehr, $tue_open,$tue_openhr,$tue_closehr,$wed_open,$wed_openhr,$wed_ +closehr, $thu_open,$thu_openhr,$thu_closehr,$fri_open,$fri_openhr,$fri_closeh +r,$sat_open,$sat_openhr,$sat_closehr ) = @$row; # -- store normal hours data in hash of hashes $normal_hrs{$days[0]}{openorclosed} = $sun_open; $normal_hrs{$days[0]}{open} = $sun_openhr; $normal_hrs{$days[0]}{close} = $sun_closehr; $normal_hrs{$days[1]}{openorclosed} = $mon_open; $normal_hrs{$days[1]}{open} = $mon_openhr; $normal_hrs{$days[1]}{close} = $mon_closehr; $normal_hrs{$days[2]}{openorclosed} = $tue_open; $normal_hrs{$days[2]}{open} = $tue_openhr; $normal_hrs{$days[2]}{close} = $tue_closehr; $normal_hrs{$days[3]}{openorclosed} = $wed_open; $normal_hrs{$days[3]}{open} = $wed_openhr; $normal_hrs{$days[3]}{close} = $wed_closehr; $normal_hrs{$days[4]}{openorclosed} = $thu_open; $normal_hrs{$days[4]}{open} = $thu_openhr; $normal_hrs{$days[4]}{close} = $thu_closehr; $normal_hrs{$days[5]}{openorclosed} = $fri_open; $normal_hrs{$days[5]}{open} = $fri_openhr; $normal_hrs{$days[5]}{close} = $fri_closehr; $normal_hrs{$days[6]}{openorclosed} = $sat_open; $normal_hrs{$days[6]}{open} = $sat_openhr; $normal_hrs{$days[6]}{close} = $sat_closehr; } ## end normal hours data foreach loop

Can anyone point me in the right direction, so that I can sort this hash of hashes according to

my $today_date = ( $today ); # start of week

my $end_date = ( $today_date + 6 ); # end of week

Or am I going at this in the wrong way? Thanks in advance!