Update: The same without need for the @day_text array. Can get the day names right from Date::Simple.#!/usr/bin/perl use strict; use warnings; use 5.014; use Date::Simple qw/ today /; my @day_text = qw(Sunday Monday Tuesday Wednesday Thursday Friday Satu +rday); my $dow = today->day_of_week; my @in_order = map {$_ % 7} $dow .. $dow + 6; say "localtime is: " . localtime; say "$_ $day_text[$_]" for @in_order; __END__ *** results of running the program C:\Old_Data\perlp>perl t5.pl localtime is: Wed Jul 10 16:10:33 2013 3 Wednesday 4 Thursday 5 Friday 6 Saturday 0 Sunday 1 Monday 2 Tuesday
The hash, %normal_hrs, could then be accessed like this sample code.my $today = today; my @names = map { ($today + $_)->strftime("%A")} 0 .. 6; my $dow = today->day_of_week; my @in_order = map {$_ % 7} $dow .. $dow + 6; say "localtime is: " . localtime; say $in_order[$_], ' ', $names[$_] for 0 .. $#in_order;
Update: For a little cleaner code, I put the contents of the 2 arrays, (@dow and @names into an array of hashes to more safely access the data below - less chance of an error.my $today = today; my @names = map { ($today + $_)->strftime("%A")} 0 .. 6; my $dow = $today->day_of_week; my @in_order = map {$_ % 7} $dow .. $dow + 6; my $today_dow = shift @in_order; my $today_name = shift @names; # using a hash slice my @today_info = @{ $normal_hrs{$today_dow} }{qw/ openorclosed open cl +ose /}; # write today's info to html # drop down list for my $dow (@in_order) { my $day_name = shift @names; # using a hash slice my @info = @{ $normal_hrs{$dow} }{qw/ openorclosed open close /}; # write this day's info to drop down list }
my $today = today; my @names = map { ($today + $_)->strftime("%A")} 0 .. 6; my $dow = $today->day_of_week; my @dow = map {$_ % 7} $dow .. $dow + 6; my @data; for my $i (0 .. 6) { push @data, {dow => $dow[$i], name => $names[$i]}; } my $date = shift @data; # using a hash slice for today's info # You have access to $date->{dow} and $date->{name} my @today_info = @{ $normal_hrs{ $date->{dow} } }{qw/ openorclosed ope +n close /}; # write today's info to html # drop down list for my $date (@data) { # You have access here to $date->{dow} and $date->{name} # using a hash slice my @info = @{ $normal_hrs{$date->{dow}} }{qw/ openorclosed ope +n close /}; # write this day's info to drop down list }
In reply to Re: Sorting Days of Week Using today () to today () + 6
by Cristoforo
in thread Sorting Days of Week Using today () to today () + 6
by Hans Castorp
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |