Hi Listers,

I've got some 2D arrays (list of lists), and I need to figure out the
neighbours of any one cell in the array. For example, imagine a list like
this:


[
 [ Q W E R T Y ],\
 [ A S D F G H ],\
 [ Z X C V B Z ],\
]


In the past, to find out the neighbours of cell in column 3, row 2 (ie the
"D"), I'd do something like this:

on getNeighbours (array, col, row)
  neighbourList = []
  -- going clockwise from top left
  neighbourList.append(array[row-1][col-1])
  neighbourList.append(array[row-1][col])
  neighbourList.append(array[row-1][col+1])
  neighbourList.append(array[row][col-1])
  neighbourList.append(array[row][col])
  neighbourList.append(array[row][col+1])
  neighbourList.append(array[row+1][col-1])
  neighbourList.append(array[row+1][col])
  neighbourList.append(array[row+1][col+1])
  return neighbourList
end

I would 'pad' the array so that the first and last row and column are full
of void values - meaning that the neighbours of the cell in the top corner
would be [<Void>, <Void>, <Void>, <Void>, "Q", "W", <Void>, "A", "S"] (this
seemed quicker that testing for each cell whether it was on an edge).

Anyway, this seems like a really clumsy approach -- but every time I sit
down to work out a better approach, I just end up doing the same thing (like
pushing a cart along the same wheel ruts). So I was hoping someone might be
able to suggest a smarter way of doing this?

Thanks in advance,
Luke



[To remove yourself from this list, or to change to digest mode, go to
http://www.penworks.com/LUJ/lingo-l.cgi  To post messages to the list,
email [EMAIL PROTECTED]  (Problems, email [EMAIL PROTECTED])
Lingo-L is for learning and helping with programming Lingo.  Thanks!]

Reply via email to