Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts

    ordering of partial weekdays in an array

    Hi,

    stuck on this one

    I have a set of days of the week that I want to output, not in the order of 0-6 but starting with a day that is determind by a start date.

    How should I make the loop of @days begin with wednesday or some other day that $start_date determines?

    Basically, how do I sequence the days like, Tuesday,Wednesday,Thursday,Friday,Saturday??

    Code:
    my $start_date = '2011-05-18'; # that's a wednesday
    my @days_of_week = qw ( Sunday Monday Tuesday Wednesday Thursday Friday Saturday );
    my @days_to_be_shown = keys %{$opening_hours_hash{$start_date}{$holiday_name}};
    
    # the array @days_to_shown is like this	  
    #@days_to_be_shown = qw( Thursday Tuesday Wednesday Friday Saturday);
    	     
      my @days=();
      
      foreach my $days ( @days_of_week){
    
          #determine which days of the week are to be outputted.
          if (grep /$days/, @days_to_be_shown ){
              push( @days, $days);
          }	
    	
      }	  
    		  
     	 
      foreach my $days (@days){
    
    #rest of loop here.
    
    }
    bazz
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #2
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    seems like this'll do the trick.

    Code:
    my $days_got = 0;
    my $first_day = 'Wednesday';
    
    foreach my $day (@days){ #these are in correct weekly order, but not beginning with the day I want as $days[0]
     
          if ( $day eq $first_day && $days_got == 0 ){
              $days_got = 1;
              push( @new_days, $day);
          }
          elsif ( $day ne $first_day && $days_got == 0 ) {
              push(@days, $day);             
          }
          elsif ( $day ne $first_day && $days_got == 1 ){
              push(@new_days, $day);
          }
      }
    That outputs Wednesday Thursday Friday Saturday
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link

  • #3
    New Coder
    Join Date
    Oct 2006
    Posts
    68
    Thanks
    0
    Thanked 28 Times in 28 Posts
    The hardest part to this is calculating the day of the week. There are a ton of modules out there that can help with that, in this instance I chose DateTime.

    After that it's just a basic array index operation to determine the days of the week that you want to display.

    Code:
    use DateTime;
    
    use strict;
    use warnings;
    
    my $start_date = '2011-05-18'; # that's a wednesday
    my @days_of_week = qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday);
    
    my ($y, $m, $d) = split '-', $start_date;
    my $dow = DateTime->new(year => $y, month => $m, day => $d)->dow();
    
    print "$dow\n"; # Prints 3.
    
    print join(',', @days_of_week[$dow..6]), "\n";
    print join(',', @days_of_week[0..$dow-1]), "\n" if $dow > 0;
    - Miller

  • #4
    Master Coder
    Join Date
    Apr 2003
    Location
    in my house
    Posts
    5,211
    Thanks
    39
    Thanked 201 Times in 197 Posts
    thanks miller, I'll look into that further, when I get my head out of its current place.

    bazz
    "The day you stop learning is the day you become obsolete"! - my late Dad.

    Why do some people say "I don't know for sure"? If they don't know for sure then, they don't know!
    Useful MySQL resource
    Useful MySQL link


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •