Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

this month followed by subsequent months

Status
Not open for further replies.

slowfish

Programmer
Aug 21, 2009
28
GB

What's the easiest way of printing this month followed by all the subsequent months, so showing 12 months in total, i.e. -

May 2010
June 2010
etc
etc
March 2011
April 2011
 
A too broad question, IMHO.
If you need the full month names, not the first three letters, you'll have to build an array with month names, then determine the current month and year by means of [tt]localtime[/tt] or [tt]gmtime[/tt] and generate the sequence with a for loop, where the only trick is to go to the start of the array and increase the year after having printed December.
Is this what you were thinking of?

Franco
: Online engineering calculations
: Magnetic brakes for fun rides
: Air bearing pads
 
Hi

It's the "trick" that I'm after.... I have an array with full month names and am using a for loop.
 
So did prex1's reply tell you what you needed? As indicated, there's no real trick here, just roll up your sleeves and start programming. Perhaps one thing you could use to make your code look a little more streamline would be to use the mod function (i.e. $month % 12) taking the result as an offset into your array. Is this for a class? Another option might be to investigate using the timelocal function.
 
This works but will probably not be considered elegant!

Code:
#!/usr/bin/perl
use strict;
use warnings;
use Date::Calc;

my ($x,$month,$sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst);
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
$mon=$mon+1;
my $count=$mon+11;
for ($x=$mon;$x<=$count;$x++) {
	if ($x>12) {
		$month = $x-12;
	} else {
		$month = $x;
	}
	for ($month) {
        if    (/^1$/)  { print "January\n" }
        elsif (/2/)  { print "February\n" }
        elsif (/3/)  { print "March\n" }
        elsif (/4/)  { print "April\n" }
        elsif (/5/)  { print "May\n" }
        elsif (/6/)  { print "June\n" }
        elsif (/7/)  { print "July\n" }
        elsif (/8/)  { print "August\n" }
        elsif (/9/)  { print "September\n" }
        elsif (/^10$/)  { print "October\n" }
        elsif (/^11$/)  { print "November\n" }
        elsif (/^12$/)  { print "December\n" }
        else          { print "Error\n" }
    } 
}
 
Think along these lines
Code:
@moy = ("Jan", "Feb", "and so on") ;
($org_mon, $org_year) = (localtime(time))[4,5] ;
$org_year += 1900 ; # you'll get 110 otherwise.
for ($i=0,$i<12,$i++) {
   $curr_mon = (($org_mon+$i) % 12) ;
   $curr_year = ($curr_mon < $org_mon) ? ($org_year + 1) : $org_year ;
   $curr_mon = $moy[$curr_mon] ;
   print "$curr_mon, $curr_year\n"
}
Obviously fill in the @moy with the way you want the month's to appear. It's untested but the general algorithm is reasonable. Tonykent's post will have trouble with the months 2 and 12. You probably meant /^2$/.
 
Code:
 $mon=$mon+1;
 $count=$mon+11;

for ( $x=$mon; $x<=$count; $x++ ) {

if ($x > 12) {
     $nextmonth = $x-12;
} else {
     $nextmonth = $x;
}

Got there in the end. Thanks for all your help!

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top