Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 如何根据数组元素的坐标改变数组元素_Ruby On Rails_Arrays_Ruby - Fatal编程技术网

Ruby on rails 如何根据数组元素的坐标改变数组元素

Ruby on rails 如何根据数组元素的坐标改变数组元素,ruby-on-rails,arrays,ruby,Ruby On Rails,Arrays,Ruby,如果我有数组中元素的坐标(|行_索引,列_索引|)如何更改元素的值? 这是我的数组:元素[1,1]==1,我需要将其附近元素的值[1,2]更改为“1” @data = [ [0,0,0,0,0], [0,1,0,0,0], [0,0,0,0,0], [0,0,0,0,0] ] 这就是我如何找到element==1的原因 @data.each_with_index do |row, row_index| row.each_with_index do |value, colum

如果我有数组中元素的坐标
(|行_索引,列_索引|)
如何更改元素的值?
这是我的数组:
元素[1,1]==1
,我需要将其附近元素的值
[1,2]
更改为
“1”

@data = [
  [0,0,0,0,0],
  [0,1,0,0,0],
  [0,0,0,0,0],
  [0,0,0,0,0]
]
这就是我如何找到element==1的原因

@data.each_with_index do |row, row_index|
  row.each_with_index do |value, column_index|
    if value == 1
      @data[row_index+1][column_index] = 1
    end
  end
end               

您只需要通过坐标直接指定值

irb(main):114:0> a[0][0] = 100
irb(main):115:0> a
=> [[100, 0, 0, 0, 0], [0, 1, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
irb(main):117:0> a[1][2] = 1
irb(main):118:0> a
=> [[100, 0, 0, 0, 0], [0, 1, 1, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]
注意:元素[1,1]与元素[1][1]不同。

元素[开始,长度]:返回一个子数组,从开始索引开始,继续到长度元素


元素[行][列]:二维数组的元素现在您的代码更改单元格下的值,该单元格包含
1
,但您已经声明需要在右侧更改它。我相信您的代码应该如下所示:

@data.each_with_index do |row, row_index|
  row.each_with_index do |value, column_index|
    if value == 1
      @data[row_index][column_index + 1] = 1
    end
  end
end
但是,您应该注意,由于您在迭代数组时正在更改数组,因此将继续向右写入
1
,直到元素完成(这将导致出现错误):

@数据=[
[0,0,0,0,0],

[0,1,1,1,1],#一种方法是执行以下操作:

@data = [
  [0,0,0,0,0],
  [0,1,0,0,0],
  [0,0,0,0,0],
  [0,0,0,0,0]
]

row = @data.find_index{ |x| x.include? 1 }
col = @data[row].find_index { |x| x == 1 }

@data[row][col.next] = 1

如果数组被更改,它将不起作用,这就是为什么我首先要找到元素==1的坐标,然后按行或行更改它附近元素的值column@AzizSharipov你应该使用元素[1][1],而不是元素[1,1]Ye,我是按照你说的那样使用的:如果value==1(在我的例子中是[1][1]),那么[row_index+1][column_index]的值应该改为“1”,所以我不能做这部分。在code
@data[row\u index+1][column\u index]=1中添加了一行,但不起作用。谢谢,这正是我需要的
@data.each_with_index do |row, row_index|
  row.each_with_index do |value, column_index|
    if value == 1 && column_index < row.size - 1
      @data[row_index][column_index + 1] = 1
    end
  end
end
all_done = false
@data.each_with_index do |row, row_index|
  row.each_with_index do |value, column_index|
    if value == 1 && column_index < row.size - 1
      @data[row_index][column_index + 1] = 1
      all_done = true
      break
    end
  end

  break if all_done
end
@data = [
  [0,0,0,0,0],
  [0,1,0,0,0],
  [0,0,0,0,0],
  [0,0,0,0,0]
]

row = @data.find_index{ |x| x.include? 1 }
col = @data[row].find_index { |x| x == 1 }

@data[row][col.next] = 1