#!/usr/bin/perl -w use strict; use mapcar; sub neighbors { my @coords= @_; my @offset= (-1) x @coords; return sub { my $i= 0; return if ! @coords; my @return= mapcar { $_[0] + $_[1] } \@coords, \@offset; while( 1 < ++$offset[$i] ) { $offset[$i]= -1; if( $#offset < ++$i ) { @offset= @coords= (); last; } } return @return; }; } my $next= neighbors( 6, 2, -4 ); my @coords; while( @coords= $next->() ) { print "( @coords )\n"; } #### ( 5 1 -5 ) ( 6 1 -5 ) ( 7 1 -5 ) ( 5 2 -5 ) ( 6 2 -5 ) ( 7 2 -5 ) ( 5 3 -5 ) ( 6 3 -5 ) ( 7 3 -5 ) ( 5 1 -4 ) ( 6 1 -4 ) ( 7 1 -4 ) ( 5 2 -4 ) ( 6 2 -4 ) ( 7 2 -4 ) ( 5 3 -4 ) ( 6 3 -4 ) ( 7 3 -4 ) ( 5 1 -3 ) ( 6 1 -3 ) ( 7 1 -3 ) ( 5 2 -3 ) ( 6 2 -3 ) ( 7 2 -3 ) ( 5 3 -3 ) ( 6 3 -3 ) ( 7 3 -3 )