Array::Slice - context-dependent array iterator


Array-Slice documentation Contained in the Array-Slice distribution.

Index


Code Index:

NAME

Top

Array::Slice - context-dependent array iterator

SYNOPSIS

Top

    use Array::Slice qw(slice);

Iterate over an array assigning several items per cycle. Three:

    while ( my ( $x, $y, $z) = slice @arr) { ... }

or two:

    while ( my ( undef, $second) = slice @arr) { ... }

or even forty-two:

    while ( @a = slice @arr, 42) { ... }

DESCRIPTION

Top

I've always wanted the power of foreach(@arr) to be applied to arrays working with more than one item at a time. Perl6 does it, Perl5 with source filtering can do it, close, but no cigar. This module is an small step towards the idea, an attempt to produce a way of slicing a single array with least obtrusive syntax I can think of.

The module works by attaching an integer counter to each scalar using perl magic API, advancing the counter on each slice.

slice @array, [ $howmany ]

Iterates over an array, returning $howmany items perl call. If called without $howmany, deduces the number depending on the calling context.

reset @array, [ $whereto ]

Resets the array iterator to $whereto or 0. $whereto can be negavtive, as in native array indexing.

BUGS

Top

Array items are copied, not aliased as in for/foreach.

Doesn't work with lists. This is one big TODO.

SEE ALSO

Top

Array::Each::Override, Array::Each, Want, List::MoreUtils, Synopsis 04(The 'for' statement).

THANKS

Top

Aaron Crane for implementation of Array::Each::Override, which code was used as base for this module.

AUTHOR

Top

Dmitry Karasik, <dmitry@karasik.eu.org>.


Array-Slice documentation Contained in the Array-Slice distribution.

# $Id: Slice.pm,v 1.11 2007/04/16 07:33:29 dk Exp $
package Array::Slice;

use strict;
use warnings;
use Want qw(howmany);

our $VERSION = '0.03';
use base qw(DynaLoader Exporter);

our @EXPORT_OK   = qw(reset slice);
our %EXPORT_TAGS = ( all => \@EXPORT_OK);

bootstrap Array::Slice $VERSION;

sub slice(\@;$) { array_slice( $_[0], $#_ ? $_[1] : howmany) }

1;