Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: How to iterate thru entire array with start point other than beginning

by GrandFather (Sage)
on Sep 02, 2016 at 01:53 UTC ( #1171029=note: print w/replies, xml ) Need Help??


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

Slices are fun:

use strict; use warnings; my @month_name = qw( January February March April May June July August September October November December ); for my $startMonthIdx (0 .. 11) { my @sel = @month_name[$startMonthIdx .. 11, 0 .. $startMonthIdx - +1]; print "$startMonthIdx: @sel\n"; }

Prints:

0: January February March April May June July August September October + November December 1: February March April May June July August September October Novembe +r December January 2: March April May June July August September October November Decembe +r January February 3: April May June July August September October November December Janu +ary February March 4: May June July August September October November December January Fe +bruary March April 5: June July August September October November December January Februa +ry March April May 6: July August September October November December January February Ma +rch April May June 7: August September October November December January February March A +pril May June July 8: September October November December January February March April Ma +y June July August 9: October November December January February March April May June Jul +y August September 10: November December January February March April May June July Augus +t September October 11: December January February March April May June July August Septemb +er October November
Premature optimization is the root of all job security

Replies are listed 'Best First'.
Re^2: How to iterate thru entire array with start point other than beginning
by soonix (Monsignor) on Sep 02, 2016 at 12:30 UTC
    no need to concatenate two ranges. Instead of
    my @sel = @month_name[$startMonthIdx .. 11, 0 .. $startMonthIdx - +1];
    you can use
    my @sel = @month_name[$startMonthIdx - 12 .. $startMonthIdx - 1];
    gives the same output as above.
Re^2: How to iterate thru entire array with start point other than beginning
by AnomalousMonk (Chancellor) on Sep 02, 2016 at 17:58 UTC

    The only objection I would make to the array slice approaches of GrandFather here or soonix here (++ to both by the way) is that they start to produce strange results for  $startMonthIdx values outside the  0 .. @month_name range. This may not be a real problem since one may never endeavor to venture outside this range.

    The attraction for me of the modulo-@month_name approach is that it gives sensible results for any weird positive or negative range whatever. I would prefer to impose any range limits in a separate step and then just reliably handle whatever range emerged from the validation process.

    c:\@Work\Perl\monks>perl -wMstrict -le "my @months = qw(Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec); ;; for my $offset (-25 .. -11) { printf 'offset %3d: ', $offset; my @sel = map $months[ ($_ + $offset) % @months ], -109 .. -95; printf qq{@sel}; print ''; } " offset -25: Nov Dec Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Ja +n offset -24: Dec Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Jan Fe +b offset -23: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Jan Feb Ma +r offset -22: Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Jan Feb Mar Ap +r offset -21: Mar Apr May Jun Jul Aug Sep Oct Nov Dec Jan Feb Mar Apr Ma +y offset -20: Apr May Jun Jul Aug Sep Oct Nov Dec Jan Feb Mar Apr May Ju +n offset -19: May Jun Jul Aug Sep Oct Nov Dec Jan Feb Mar Apr May Jun Ju +l offset -18: Jun Jul Aug Sep Oct Nov Dec Jan Feb Mar Apr May Jun Jul Au +g offset -17: Jul Aug Sep Oct Nov Dec Jan Feb Mar Apr May Jun Jul Aug Se +p offset -16: Aug Sep Oct Nov Dec Jan Feb Mar Apr May Jun Jul Aug Sep Oc +t offset -15: Sep Oct Nov Dec Jan Feb Mar Apr May Jun Jul Aug Sep Oct No +v offset -14: Oct Nov Dec Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov De +c offset -13: Nov Dec Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Ja +n offset -12: Dec Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Jan Fe +b offset -11: Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec Jan Feb Ma +r


    Give a man a fish:  <%-{-{-{-<

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1171029]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (2)
As of 2018-07-22 14:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (454 votes). Check out past polls.

    Notices?