Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^2: Sorting Days of Week Using today () to today () + 6

by Cristoforo (Deacon)
on Jul 11, 2013 at 13:52 UTC ( #1043731=note: print w/ replies, xml ) Need Help??


in reply to Re: Sorting Days of Week Using today () to today () + 6
in thread Sorting Days of Week Using today () to today () + 6

I don't think you need that hash %normal_hrs as you have all the info in the returned SQL query. So, I think this version will be simpler without the hash initialization. Also, I made a minor change to the way the dates, (day_of_week and names), were created.

# -- 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; my @row = $sth->fetchrow->array; my $today = today; my @day = map $today + $_, 0 .. 6; my @data; for my $i (0 .. 6) { push @data, {dow => $day[$i]->day_of_week, name => $day[$i]->strftime("%A")}; } my $date = shift @data; # you have $date->{name} here too my $i = 3 * $date->{dow}; my @today_info = @row[$i .. $i+2]; # write today's info to html # drop down list for my $date (@data) { # You have access here to $date->{dow} and $date->{name} my $i = 3 * $date->{dow}; my @info = @row[$i .. $i+2]; # write this day's info to drop down list }


Comment on Re^2: Sorting Days of Week Using today () to today () + 6
Select or Download Code
Re^3: Sorting Days of Week Using today () to today () + 6
by Hans Castorp (Acolyte) on Jul 17, 2013 at 15:38 UTC

    Cristoforo,

    Thank you so much for all your help. I am going to keep working on this but I came up against my deadline and had to find another solution. I'm going to respond to my original post with what I ended up doing. I have soooo much to learn!

    Warm regards, HC

      Hans Castorp

      I'm afraid I've muddied up the waters with any (badly) supplied ideas. Sorry if I've only added to the confusion :-(

      I'll look over your latest post.

      Update: I found an extra right brace at the very end of your program that should be removed.

      } ## end format time sub } <- remove this brace

      Maybe the code below will help. I don't know if hash slices is the answer or not - depends on how they're used. I eliminated the %this_wk hash and the %hrs and added a subroutine, process. Its difficult to tell if this may work as I don't have a database to test it against.

      #!/usr/bin/perl use strict; use warnings; use DBI; use Date::Simple qw(today); use Time::Format qw(%time time_format %strftime %manip); use Data::Dumper; use Skidmore::Utils qw(setOracleEnv); # -- call sub to set Oracle env so script can run as cronjob &setOracleEnv; # -- define outfile my $htmlout = "/www/includes/lib_hours2.html"; #connection info for webprod my ($dsn,$dbun,$dbpass) = ( 'dbi:Oracle:host=;sid=;port=','','' ); # -- define today my $today_date = today; my $end_date = $today_date + 6; # -- connect to Webprod db my $dbh = DBI->connect( $dsn,$dbun,$dbpass, { AutoCommit => 0, RaiseEr +ror => 0, PrintError => 1 } ) || die $DBI::errstr; # -- query to get hours for the upcoming week my $sql = qq(SELECT TO_CHAR(libdate,'YYYY-MM-DD'),openhour,closehour FROM hours WHERE TO_CHAR(libdate,'YYYY-MM-DD') BETWEEN '$today_date' AND '$end_da +te'); my $sth = $dbh->prepare( $sql ); $sth->execute; # -- fetch all data and create var to refer to my $arrayref_hrs = $sth->fetchall_arrayref( ); $sth->finish; $dbh->disconnect; my $today = shift @$arrayref_hrs; my $href = process($today); my $content; # -- write today's hours if ( $href->{open} eq 'closed') { $content = qq(<p class="scribsmalltext">Today the library is close +d</p>); } else { $content = qq(<p class="scribsmalltext">Today's hours are $href->{ +open} - $href->{close}</p>); } $content .= qq(<table style="height:80px;" class="scribsmalltext" widt +h="200" border="0"><tbody>); foreach my $row ( @$arrayref_hrs ) { my $href = process($row); if ( $href->{open} eq 'closed') { $content .= "<tr><td>$href->{date}</td><td>Closed</td></tr>"; + } else { $content .= "<tr><td>$href->{date}</td><td>$href->{open} - $hr +ef->{close}</td></tr>"; } ## end if } # -- close the html table and print all html to file $content .= qq(</tbody></table>); open OUTFILE, ">$htmlout" or die $!; print OUTFILE $content; close OUTFILE; # -- Subs start here # -- sub format_time { my $time = shift; # if $time > 2400, this will 'normalize' it to less than 2400 # otherwise if $time < 2400, it will remain the same my $formatted_time = $time % 2400; # -- time function (from Time module) needs time string in this fo +rmat: hh:mm:ss # -- our time string is like 800 or 2300 so we must do some format +ting to it # -- first, pad with zero if only 3 character $formatted_time = sprintf( "%04s", $formatted_time ); # -- second, add two zeros to end of time string - seconds $formatted_time = $formatted_time .'00'; # -- third, add ":" separators $formatted_time =~ s/(\d\d)(\d\d)(\d\d)/$1:$2:$3/; # -- now use time function for final formatting $formatted_time = time_format('H:mm am', "$formatted_time"); # -- for some reason "0" showing in times less than 10 even though + format for time format looks correct # -- let's remove it now $formatted_time =~ s/^0//; # -- return formatted time string return $formatted_time; } ## end format time sub sub process { my $row = shift; my %hash; my ( $libdate,$open_hr,$close_hr ) = @$row; if (defined $open_hr) { $hash{open} = format_time($open_hr); } else { $hash{open} = 'closed'; } if (defined $close_hr) { $hash{close} = format_time($close_hr); } $hash{date} = time_format( 'Day Month dth', $libdate ); return \%hash; }

        Oh, no--your input is awesome. I'm just going to have to learn more before I can understand how to use it! Ugh--I thought I had solved my problem, but realized what I thought was returning "today's hours" was actually returning the first value in the unsorted hash. So, today I am reading up on hash slices. I think this may be what I need to grab one value from a hash?

        I value this forum so much because I can read and read and read about Fred and Barney and Dino (;-), and try stuff, and after awhile I feel like I'm just moving stuff around randomly and getting nowhere. It helps to have someone who knows look at it. Thank you very much for all your thoughtful input.

        Hi Cristoforo,

        This is perfection--gives today's hours, the following 6 days in a table below. Beautiful! Thank you!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1043731]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (14)
As of 2014-07-22 18:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (123 votes), past polls