Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

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

by grinder (Bishop)
on Jan 21, 2002 at 20:58 UTC ( #140429=note: print w/replies, xml ) Need Help??


in reply to (tye)Re: getting my neighbours in an N-dimensional space
in thread getting my neighbours in an N-dimensional space

Oh well done! This is exactly what I was waffling on about. Except that as it stands, your code returns the point of origin, and the origin can't be a neighbour of itself.

--
g r i n d e r
print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u';
  • Comment on Re:x2 getting my neighbours in an N-dimensional space

Replies are listed 'Best First'.
(tye)Re2: getting my neighbours in an N-dimensional space
by tye (Sage) on Jan 21, 2002 at 21:04 UTC

    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://140429]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2021-05-17 18:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (159 votes). Check out past polls.

    Notices?