This program is free software; you can redistribute it and/or modify
it under the same terms as Perl 5 itself.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Perl 5 License schemes for more details.
contact the author via http://www.greglondon.com
NAME
Iterate - Smart, Simple, Recursive Iterators for Perl programming.
SYNOPSIS
use Iterate;
# iterate an array, at index 3, change the value in the array to "three"
my @array = qw (alpha bravo charlie delta echo);
IterArray @array, sub
{
# $_[1] is the current numeric index
if($_[1] == 3)
{
# modify the element in the original array
$_[0] = 'three'; # current element available via $_[0]
}
}
# iterate a hash, perform nested iteration on the same hash.
my %hash =
(
blue => 'moon',
green => 'egg',
red => 'baron',
);
IterHash (%hash, sub
{
my $key1 = $_[0];
my $val1 = $_[1];
print "checking key1 $key1, val1 $val1 for collisions \n";
IterHash (%hash, sub
{
my $key2 = $_[0];
my $val2 = $_[1];
print "\tchecking key2 $key2, val2 $val2 for collisions \n";
print "\t $val2 is not $key1\n"
unless($key1 eq $key2);
return;
});
});
# iterate a file, read it line by line, and grep for a string.
IterFile "tfile.pl", sub
{
# the line read from the file is stored in $_[0]
my $line = $_[0];
# the current line number corresponding to $_[0] is stored in $_[-1]
my $number = $_[-1];
if($line =~ /search/)
{
print "found at line $number: $line";
}
};
DESCRIPTION
This module is intended to demonstrate a simple way to implement iterators on perl variables with little code required of the programmer using them.
Some additional advantages over standard perl iterators:
Array iterators give access to the current index within the array. Hash iterators can be nested upon the same hash without conflicts. File iterators allow simple file munging in a few lines of code.
EXPORT
IterArray
IterHash
IterFile
AUTHOR
Iterate - Smart, Simple, Recursive Iterators for Perl programming.
Copyright (C) 2002 Greg London
This program is free software; you can redistribute it and/or modify
it under the same terms as Perl 5 itself.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Perl 5 License schemes for more details.
contact the author via http://www.greglondon.com
SEE ALSO
perl(1).