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";
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Array iterator factory
by ihb (Deacon) on Mar 17, 2005 at 12:02 UTC | |
by tye (Sage) on Mar 17, 2005 at 16:46 UTC | |
by ihb (Deacon) on Mar 17, 2005 at 19:12 UTC | |
by Roy Johnson (Monsignor) on Mar 17, 2005 at 12:13 UTC | |
Re: Array iterator factory
by tlm (Prior) on Mar 16, 2005 at 18:54 UTC | |
by Roy Johnson (Monsignor) on Mar 16, 2005 at 19:30 UTC | |
by tye (Sage) on Mar 17, 2005 at 03:25 UTC | |
by Roy Johnson (Monsignor) on Mar 17, 2005 at 04:55 UTC | |
by tye (Sage) on Mar 17, 2005 at 16:53 UTC |
Back to
Cool Uses for Perl