Here's one approach...

--------
on getNeighbors lst, row, col
  rows = lst.count
  cols = lst[1].count
  tempList = []
  repeat with x = max(row - 1,1)  to min (row + 1,rows)
    repeat with y = max(col-1,1) to min (col + 1,cols)
      append templist, lst[x][y]
    end repeat
  end repeat
  templist.deleteOne(lst[row][col]) -- Neighbors Only
  return tempList
end
--------

This should work with values that are out of range.

Regards,
Brian Ellertson


-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED]]On
Behalf Of Luke Wigley
Sent: Wednesday, February 28, 2001 12:34 AM
To: [EMAIL PROTECTED]
Subject: <lingo-l> list of lists - determining neighbors


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!]



[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