Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

(tye)Re2: getting my neighbours in an N-dimensional space

by tye (Sage)
on Jan 21, 2002 at 21:04 UTC ( #140431=note: print w/replies, xml ) Need Help??


in reply to Re:x2 getting my neighbours in an N-dimensional space
in thread getting my neighbours in an N-dimensional space

Yeah, I mentioned that. Here is one that fixes that and is actually simpler. (:

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

        - tye (but my friends call me "Tye")

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://140431]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2021-05-12 13:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (127 votes). Check out past polls.

    Notices?