in reply to How to iterate thru entire array with start point other than beginning

#!/usr/bin/perl -l # http://perlmonks.org/?node_id=1170987 use strict; use warnings; my @month_name = qw(January February March April May June July August +September October November December); for my $start (0 .. 11) { my @wanted = (@month_name, @month_name)[$start .. $start + 11]; print "@wanted"; }
  • Comment on Re: How to iterate thru entire array with start point other than beginning
  • Download Code

Replies are listed 'Best First'.
Re^2: How to iterate thru entire array with start point other than beginning
by dirtdog (Monk) on Sep 01, 2016 at 18:21 UTC

    I spoke too soon. I'm actually a little stumped on the % modulo operator and the example. I see a lot more than 12 months being printed out. The goal is to start with current month +1 (so October if using today in the example) and then print the 12 months in order from there..

    #!/usr/bin/perl use POSIX 'strftime'; use strict; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime; my $curr_mth = sprintf "%2d", strftime('%m', $sec,$min,$hour,$mday,$mo +n,$year,$wday,$yday,$isdst); my @month_name = qw(January February March April May June July August +September October November December); for my $m ( 0 .. $#month_name) { print "Month name is: $month_name[$m]\n"; }

    I'm trying to get out as follows:

    Month name is: October Month name is: November Month name is: December Month name is: January Month name is: February Month name is: March Month name is: April Month name is: May Month name is: June Month name is: July Month name is: August Month name is: September

    then next month the output would be:

    Month name is: November Month name is: December Month name is: January Month name is: February Month name is: March Month name is: April Month name is: May Month name is: June Month name is: July Month name is: August Month name is: September Month name is: October

      If you want to use the % operator, it can be quite simple. To expand on the GP anonymonk's script:

      #!/usr/bin/env perl use strict; use warnings; # Set an array of names my @names = qw/January February March April May June July August September October November December/; # Find the index number of next month my $next = (localtime)[4] + 1; # Loop over the next 12 months and print the month names in sequence for my $monpos ($next .. $next + 11) { print "Month is $names[$monpos % 12]\n"; }

      Please ask if anything in this piece of code is unclear.

        Thanks Hippo. It does seem simple now that I understand it!

      I have some code that works, but boy o' boy is it ugly.

      #!/usr/bin/perl use POSIX 'strftime'; use strict; my $m; my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime; my $curr_mth = sprintf "%2d", strftime('%m', $sec,$min,$hour,$mday,$mo +n,$year,$wday,$yday,$isdst); my @month_name = qw(January February March April May June July August +September October November December); for ( $curr_mth .. $#month_name) { print "Month name is: $month_name[$_]\n"; $m++; } if ( $m < 12 ) { for my $n ( 0 .. ($#month_name - $m) ) { print "Month name is: $month_name[$n]\n"; } }
        but boy o' boy is it ugly

        I always say, first make it work. THEN make it pretty. ;-)

        HTH,

        planetscape