Forum: Ruby How to find a single cell in a 2D array?

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
203e551d46adb79d1f4cfbb1b01eb171?d=identicon&s=25 Charles W. (charles_w)
on 2016-11-30 17:59
I have defined a method to read any one of multiple maze files.

maze = []
def maze_reader
  mz_file ="")
  while ! mz_file.eof?
    line = mz_file.gets.chomp.split(" ")
    maze << line

Next I need to create a method to find specific letters in the maze, say
S for start and F for finish.

I have listened to tutorials; however, I have a learning problem that
often prevents me from memorizing spoken words, and the book I am using
does not appear to cover this yet.
Is there anyone who can point me in the right direction?
9627ec27e4fb3a18be02133b4ddaa901?d=identicon&s=25 Joel P. (virtuoso)
on 2016-12-04 11:25
This will find the first matching location in a 2D array of Strings.

maze = [['S',' ',' '],[' ',' ',' '],[' ',' ','F']]

def find_location( maze, location )
  # Go through each row
  maze.each_index do |i|
    # Find the location in the row
    result = maze[i].index( location )
    # Return the row index and location index if found
    return [i, result] if result
  # return nil if nothing was found
  return nil

find_location maze, 'S'
=> [0, 0]
find_location maze, 'F'
=> [2, 2]
9d2d160377d47a2144b12d36ab63126f?d=identicon&s=25 Regis d. (raubarede)
on 2016-12-05 15:31
class Array
  def rfind(sae,aa=[])
    each_with_index() { |a,i|
      r= a.kind_of?(Array) ? a.rfind(sae,aa+[i]) : a==sae ? aa+[i] :
      return r if r

p a.rfind(3)
p a.rfind(9)

p a.rfind(9)

p a.rfind(9)

[3, 1]
[3, 2, 2]
This topic is locked and can not be replied to.