Loop






Pair Networks
Loop

NAME

Loop - Smart, Simple, Recursive Iterators for Perl programming.

SYNOPSIS

  use Loop;
  
  Loop::Array @array, sub
	{
	my ($index,$value)=@_;
	print "at index '$index', value='$value'\n";
	}

ABSTRACT

This module is intended to implement simple 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.

DESCRIPTION

Loop on an Array

  # loop on an array, at index 3, change the value in the array to "three"
  my @array = qw (alpha bravo charlie delta echo);

  Loop::Array @array, sub
	{
	my($index,$val)=@_;
	if($index == 3)
		{
		# modify the element in the original array
		# note that when you want to change the original array,
		# you must assign to the parameter array @_
		$_[1] = 'three'; 
		}
	}

Loop on a Hash

  # loop on a hash, perform nested iteration on the same hash.

  my %hash = 
	(
	blue => 'moon',
	green => 'egg',
	red => 'baron',
	);
 
  Loop::Hash (%hash, sub
	{
	my($key1,$val1)=@_;

	print "checking key1 $key1, val1 $val1 for collisions \n";

	Loop::Hash (%hash, sub
		{
		my($key2,$val2)=@_;

		print "\tchecking key2 $key2, val2 $val2 for collisions \n";

		print "\t $val2 is not $key1\n"
			unless($key1 eq $key2);
		return;
		});
	});

Loop on a File

  # loop through a file, read it line by line, and grep for a string.
  Loop::File "tfile.pl", sub
	{
	my($linenum,$linestr)=@_;

	if($linestr =~ /search/)
		{
		print "found at line $linenum: $linestr";
		}
	};

EXPORT

none

SEE ALSO

AUTHOR

Greg London, http://www.greglondon.com

COPYRIGHT AND LICENSE

Copyright 2003 by Greg London, All Rights Reserved

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