The solutions already given are very compact and it seems to me that this is cluttering and obscuring a nice solution. As a matter of fact, I don't really understand the solutions given. (So that's my problem you say.) So I come up with an other solution, which to my is much clearer. This is it:
#! /usr/local/bin/perl @coord = ( 6, 2, -4 ); $dim = scalar ( @coord ); @trans = map { $_ - 1 } @coord; for ( $i = 0; $i < 3**$dim; $i++ ) { @vector = vector ( $i ); @add = add_vector ( \@trans, \@vector ); print "[@add]\n" unless center ( @vector ); } sub vector { my $i = shift @_; my @vector; for ( my $j=0; $j < $dim; $j++ ) { push ( @vector, $i % 3 ); $i = int $i/3; } return @vector; } sub add_vector { my $ar_ref1 = shift @_; my $ar_ref2 = shift @_; my @vector; for ( my $j=0; $j < $dim; $j++ ) { push ( @vector, $$ar_ref1[$j] + $$ar_ref2[$j] ); } return @vector; } sub center { my @vector = @_; my $bool = 1; my $i = 0; while ( $bool and $i < $dim ) { $bool = ( $vector[$i++] == 1 ); } return $bool; }
with the given coordinates it produces the following output
[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] [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]
I am not going to explain my code and the idea's I used to create it. (It would be rather lengthy). The variables and subroutines I use suggest the underlying mechanisme. But if my code is as obscure to you as the others solutions are to my, please comment on the reply and I will try to explain it!

In reply to Re: getting my neighbours in an N-dimensional space by Anonymous Monk
in thread getting my neighbours in an N-dimensional space by dash2

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.