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

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

by Hans Castorp (Sexton)
on Jul 17, 2013 at 15:38 UTC ( [id://1044849]=note: print w/replies, xml ) Need Help??


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

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

Replies are listed 'Best First'.
Re^4: Sorting Days of Week Using today () to today () + 6
by Cristoforo (Curate) on Jul 17, 2013 at 20:05 UTC
    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!

        Hi Hans Castorp

        I'm happy it was useful for you. I think the process() function was helpful for making the program easier to follow.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2024-03-28 12:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found