NAME

Array::Iterator - A simple class for iterating over Perl arrays

VERSION

Version 0.133

SYNOPSIS

use Array::Iterator;

# create an iterator with an array
my $i = Array::Iterator->new(1 .. 100);

# create an iterator with an array reference
my $i = Array::Iterator->new(\@array);

# create an iterator with a hash reference
my $i = Array::Iterator->new({ __array__ => \@array });

# a base iterator example
while ($i->has_next()) {
    if ($i->peek() < 50) {
        # ... do something because
        # the next element is over 50
    }
    my $current = $i->next();
    # ... do something with current
}

# shortcut style
my @accumulation;
push @accumulation => { item => $iterator->next() } while $iterator->has_next();

# C++ ish style iterator
for (my $i = Array::Iterator->new(@array); $i->has_next(); $i->next()) {
  my $current = $i->current();
  # .. do something with current
}

# common perl iterator idiom
my $current;
while ($current = $i->get_next()) {
  # ... do something with $current
}

DESCRIPTION

This class provides a very simple iterator interface. It is uni-directional and can only be used once. It provides no means of reversing or resetting the iterator. It is not recommended to alter the array during iteration, however no attempt is made to enforce this (although I will if I can find an efficient means of doing so). This class only intends to provide a clear and simple means of generic iteration, nothing more (yet).

METHODS

Public Methods

Protected Methods

These methods are protected, in the Java/C++ sense of the word. They can only be called internally by subclasses of Array::Iterator, an exception is thrown if that condition is violated. They are documented here only for people interested in subclassing Array::Iterator.

TO DO

SEE ALSO

This module now includes several subclasses of Array::Iterator which add certain behaviors to Array::Iterator, they are:

The Design Patterns book by the Gang of Four, specifically the Iterator pattern.

Some of the interface for this class is based upon the Java Iterator interface.

OTHER ITERATOR MODULES

There are a number of modules on CPAN with the word Iterator in them. Most of them are actually iterators included inside other modules, and only really useful within that parent modules context. There are however some other modules out there that are just for pure iteration. I have provided a list below of the ones I have found, if perhaps you don't happen to like the way I do it.

ACKNOWLEDGEMENTS

ORIGINAL AUTHOR

stevan little, stevan@iinteractive.com

ORIGINAL COPYRIGHT AND LICENSE

Copyright 2004, 2005 by Infinity Interactive, Inc.

http://www.iinteractive.com

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SUPPORT

This module is provided as-is without any warranty.

Please report any bugs or feature requests to bug-array-iterator at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Array-Iterator. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.

You can find documentation for this module with the perldoc command.

perldoc Array::Iterator

You can also look for information at: