Array-Each
view release on metacpan or search on metacpan
lib/Array/Each.pm view on Meta::CPAN
Get the value with get_undef(), e.g.,
my $undef_val = $obj->get_undef();
Any value is valid for C<undef>.
=item C<stop>, set_stop( INDEX ), get_stop()
The C<stop> attribute tells each() where to stop its iterations. By
default, C<stop> is undefined, meaning each() will stop where it wants,
depending on C<bound>, C<group>, and the sizes of the arrays.
If C<bound> is true and C<stop> is set higher than C<$#shortest_array>,
then C<stop> will have no effect (it will never be reached). If it is
set lower, then the iteration will stop I<after> that element has been
returned by each().
If C<bound> is false and the C<stop> value is defined, then the
iteration will stop I<after> that element has been returned,
lib/Array/Each/Tutorial.pod view on Meta::CPAN
=head1 SOME ATTRIBUTE COMBINATIONS
This section shows how some attribute combinations will affect what
each() returns.
=head2 Iterator and Rewind
Setting C<iterator> will determine the starting point of the very
next iteration. Setting C<rewind> will determine where the rewind()
method (and internal rewind operations) will rewind to. Setting
them both to the same value will cause successive iterations to
start at the same place as the first one.
In the following hypothetical situation, the first array element
has a special meaning, while the remaining ones are meaningful
pairs. So we want to skip the first element when we print out the
pairs by setting both C<iterator> and C<rewind> to 1.
my @a = ( [d=>4], a=>1, b=>2, c=>3, d=>4 );
my $obj = Array::Each->new( set=>[\@a],
iterator=>1, rewind=>1, group=>2 );
lib/Array/Each/Tutorial.pod view on Meta::CPAN
print "$k => $v\n";
}
push @a, @{$a[0]} = ( e=>5 );
# rewind affects where this starts
while( my( $k, $v ) = $obj->each ) {
print "$k => $v\n";
}
=head2 Group and Undef
By default the C<bound> attribute is true. This means that iterations
will stop when the end of the shortest (or only) array is reached.
However, if C<group> is set greater than 1, and the size of the
shortest array is not a multiple of the C<group> value, then the
last iteration will "go beyond" the end of the shortest array.
When this happens, each() will return the value of the C<undef>
attribute for the "missing" array elements.
By default, the C<undef> attribute is undefined. So each() will
return perl's undef for "missing" or "non-existent" array elements.
lib/Array/Each/Tutorial.pod view on Meta::CPAN
print qq{<table border="1">\n};
while( my @row = $obj->each ) {
printf "<tr> <td>%d.</td> ", pop @row;
print map( "<td>$_</td> ", @row ), "</tr>\n";
}
print "</table>\n";
=head2 Bound and Undef
As stated above, C<bound> is true by default. If you set it to 0
(false), the iterations are no longer bound by the size of the
shortest array. Instead they are bound by the size of the largest
array (but see C<stop> to change that).
So if you have more than one array, and they are not the same size,
each() will return the value of C<undef> for the "missing" array
elements. If the default value (perl's undef) is not acceptable,
setting C<undef> lets you choose what you want.
The following example shows how you might create a text table giving
the sums of elements of three arrays. Because the arrays are
( run in 1.028 second using v1.01-cache-2.11-cpan-96521ef73a4 )