Inspired by the request in Foreach Loops and finding Array::Iterator modules not to my liking, I whipped up a little array iterator factory. It gave me a reason to do a little dabbing with pod, a little dabbling with warnings. I feel enriched.

Updates: incorporated suggestions from ihb regarding function call syntax and warnings.

use strict; use warnings; =pod =head1 NAME array_iterator - a factory for iterators =head1 SYNOPSIS my $iter = array_iterator(@array); while (my ($i, $data) = $iter->()) { # was &$iter, see ihb's respons +e # $i is index, $data is data } $iter->(0); # Reset to start of array $iter->(-1); # Reset to end of array, and iterate backwards $iter->(5); # Reset $i to 5 =head1 NOTES This is an accessor only. It will not create array elements. You can do that by operating directly on the array. After running out, it implicitly resets to front or back end of the array, depending on which direction it was iterating. =cut sub array_iterator (\@) { my $aref = shift; my $i = 0; sub { if (@_) { # no need for use warnings::register; $i = shift; if ($i > $#$aref) { $i = $#$aref; warnings::warnif(misc => "index beyond end of array"); } elsif ($i < -@$aref) { $i = -@$aref; warnings::warnif(misc => "array index excessively nega +tive"); } } elsif ($i >= -@$aref and $i <= $#$aref) { my $i_was = $i; $i += $i < 0 ? -1 : 1; ($i_was, $aref->[$i_was]); } else{ $i = ($i > $#$aref) ? 0 : -1; return (); } } } my @arr = (20..30); my $I1 = array_iterator(@arr); print "Skipping every 4th...\n"; while (my ($i, $data) = &$I1) { next if $i % 4 == 3; print "$i: $data\n"; } print "Running through again, without skips:\n"; while (my ($i, $data) = $I1->()) { # was &$I1 here and below print "$i: $data\n"; } print "-- Now...backwards! --\n"; $I1->(-1); while (my ($i, $data) = $I1->()) { print "$i: $data\n"; } print "again, but only printing every 3rd:\n"; while (my ($i, $data) = $I1->()) { print "$i: $data\n" if $i % 3 == 0; } print "Boundary check:\n"; $I1->(-50); while (my ($i, $data) = $I1->()) { print "$i: $data\n"; } print "This should print nothing:\n"; my @empty = (); my $I2 = array_iterator(@empty); print join(':', $I2->()), "\n";

In reply to Array iterator factory by Roy Johnson

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.