Here's the basics for another approach. (You may extend it the way you feel fit.)

{ package Array::Grouped; sub TIEARRAY { bless { n => $_[1], array => $_[2] } => $_[0] } sub FETCHSIZE { @{$_[0]{array}} / $_[0]{n} } sub FETCH { my $self = shift; my ($i) = @_; my $n = $self->{n}; return sub { \@_ }->(@{$self->{array}}[$i*$n .. $i*$n + $n + - 1]); } } my @bar = 0 .. 9; tie my @foo, Array::Grouped::, 2, \@bar; for (@foo) { $_->[0] = 'x'; } print @bar; # x1x3x5x7x9 ### Update: sub by_groups_of { tie my @foo, Array::Grouped::, @_ } my $foo = by_groups_of(2, \@bar); for (@$foo) { $_->[0] = 'y'; } print @bar; # y1y3y5y7y9
The advantage with this approach is it's memory efficiency and ease of use. If you just want to know e.g. the last or a specific interval you can do that using regular slices. It doesn't consume a lot of memory since it uses the original array and produces the groups when needed.

An overall problem with both this solution and your solution is what to do when the original array is modified, and in particular, spliced upon. Since the problem may be easily solved by hand and you then can explicitly define how you want to handle it, I'm afraid that an abstraction soon will end up with so many options and flags (or limitations) you'd end up with something more complicated than the hand-rolled solution and it's a loss using it. "Over-engineered" comes to mind. That's not to say there are no cases where an abstraction like this is suited.

Personally I find this tie solution to be overkill for everyday use and I would most likely either use my simple &group (see Re: loop thru 3-element array?) or manually set up a for (to handle edge cases). My &group has proven to cover my needs well while being extremely simple. Actually, it's sub pair { group(2, @_) } I most often use and that's when I want to use an each analogy for lists.

Update: Added a wrapper subroutine to show it's just as easy to use as of the other functions in this thread.

ihb

Read argumentation in its context!


In reply to Re: How to make references more like aliases? by ihb
in thread How to make references more like aliases? by Limbic~Region

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.