in reply to Re^2: finding neighbors of an element in arrays
in thread finding neighbors of an element in arrays

And you just want the number? And this is a regular matrix without holes, i.e. all the arrays @domiansX have the same number of elements and no empty cells? Then all elements inside the matrix have 4 neighbors, all elements at the sides have 3 and all elements at the edge of the matrix have 2 neighbors.

If your matrix has holes, you might preset each element to have 4 neighbors and subtract 1 from every neighbor of a hole

PS: It might be easier to store the matrix as a two-dimensional array instead of having each row in a separate array. Check out perllol how to use two-dim arrays aka ArrayOfArrays

Ok, misread your examples. But I still don't get it, why is PF00389.24 not a neighbor of PF11001.3 ? Maybe you could define neighborhood directly instead of using examples

  • Comment on Re^3: finding neighbors of an element in arrays

Replies are listed 'Best First'.
Re^4: finding neighbors of an element in arrays
by persianswallow (Novice) on Jul 28, 2011 at 09:27 UTC

    order in array was important and i arranged them in special order.assume @domain={A,B,C,D},B is a neighbor for A and A and C are neighbors for B.neighbors are elements around an specific element.thanks a lot for your help.

      Ok, your script is nearly there, but you need to be able to store many neighbors for each element, so instead of a hash you need a HashofHashes, see perllol. Just change lines like $h{$element}=$domains1[1]; and replace them with $h{$element}{$domains1[1]}++;

      Why do I have the impression this is just homework? Maybe because a similar question, just with words instead of domains, was posted inside an hour of this one.

Re^4: finding neighbors of an element in arrays
by persianswallow (Novice) on Jul 28, 2011 at 09:42 UTC

    my @domains1=('PF05.3','PF11001.3','PF00389.24','PF10417.3'); my @domains2=('PF01','PF02','PF11001.3','PF00389'); my @domains3=('PF00389.24','PF05.3','PF01','PF00389'); as you see,for example PF00389.24 in @domains1 has two neighbor 'PF11001.3','PF10417.3' and in @domain2 nothing and in @domain3 one(PF05.3).

      I was refering to your example above. You said "PF11001.3 has 3 neighbors", but it seems it has 4 neighbors, since PF00389.24 is also a neighbor of PF11001.3