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";
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.