All,
In this node, I provided a solution to loop over an array by an arbitrary number of elements each iteration. The problem is that it produces copies and not aliases like you would get with a for loop. I was able to modify the code to use references, but I am not very happy with it.
#!/usr/bin/perl use strict; use warnings; my @array = 0 .. 30; my $next = by_groups_of( 3, @array ); while ( my @group = $next->() ) { ${ $group[1] } = 'foo'; # This is FUGLY } print "$_\n" for @array; sub by_groups_of { my $by = shift; return sub { () } if ! $by || $by =~ /\D/ || ! @_; my $list = \@_; my $pos = 0; my $done; return sub { return () if $done; my $start = $pos; my $stop = $pos + $by - 1 > $#$list ? $#$list : $pos + $by - 1 +; $pos += $by - 1; $done = 1 if $stop == $#$list; return map { \$_ } @$list[ $start .. $stop ]; } }
Is there a way to make this more DWYMish without resorting to scary hacks? Not that scary hacks aren't welcome, but I have production code that I wouldn't mind using it in.

Cheers - L~R

Since I didn't want to modify the original code here, see this node, for a copy of the latest revision

In reply to 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.