如何在ruby中循环嵌套数组?

时间:2021-12-02 15:40:55

I'm creating a grid using a 2-d array and i want to put different values in it I have tried:

我正在使用二维数组创建一个网格,我想在其中添加不同的值我尝试过:

grid =(Array.new(10,Array.new(10," ") ))
for row in rand(1..9)
     for column in rand(1..9)
        grid[row][column] == 'a'

2 个解决方案

#1


2  

grid = Array.new(10) { Array.new(10, "") }

otherwise you'll get the same array repeated 10 times, which is presumably not what you want.

否则你会重复10次相同的阵列,这可能不是你想要的。

I am not sure what you want to do with your iterations. Do note that Ruby arrays are 0-indexed, so a 10-element array will have an index in 0...9. Also note that iteration over an array is usually done with each, as Carpetsmoker notes in comments:

我不确定你想要用你的迭代做什么。请注意,Ruby数组是0索引的,因此10个元素的数组将具有0 ... 9的索引。另请注意,对于数组的迭代通常是通过每个进行的,正如Carpetsmoker在评论中所说:

grid.each do |row|
  row.each_index do |index|
    row[index] = "abcde"[rand(5)]
  end
end

EDIT: Thanks Cary!

编辑:谢谢卡里!

#2


1  

Your question is not clear. If you want to put the letter 'a' at n random locations in grid, you could do this:

你的问题不明确。如果你想把字母'a'放在网格中的n个随机位置,你可以这样做:

def salt(grid, obj, n)
  m = grid.size
  locs = (0...m*m).to_a.sample(n)
  locs.each do |l|
    row, col = l.divmod(m)
    grid[row][col] = obj
  end
end

grid = Array.new(10) { Array.new(10, ' ') }

salt(grid,'a',30)
grid
  #=> [[" ", "a", " ", " ", "a", "a", "a", " ", " ", " "],
  #    ["a", "a", " ", "a", "a", " ", " ", "a", " ", " "],
  #    [" ", "a", " ", " ", "a", " ", " ", " ", "a", " "],
  #    [" ", " ", " ", "a", " ", " ", "a", " ", " ", "a"],
  #    [" ", " ", " ", "a", " ", " ", " ", " ", " ", " "],
  #    [" ", " ", "a", " ", " ", " ", " ", " ", " ", " "],
  #    ["a", " ", " ", " ", " ", " ", "a", " ", " ", "a"],
  #    [" ", " ", "a", " ", " ", "a", " ", "a", " ", " "],
  #    [" ", "a", " ", " ", "a", " ", " ", " ", "a", "a"],
  #    [" ", " ", "a", "a", "a", " ", " ", " ", " ", " "]]

You could instead write:

你可以写:

locs =  n.times.map { rand(m*m) }

but that will likely result in some duplicates, in which case fewer than n cells will be filled with "a"'s. For example, when I computed locs that way for n=30 I found:

但这可能会导致一些重复,在这种情况下,少于n个单元格将填充“a”。例如,当我计算n = 30的locs时,我发现:

locs.uniq.size
  #=> 27

#1


2  

grid = Array.new(10) { Array.new(10, "") }

otherwise you'll get the same array repeated 10 times, which is presumably not what you want.

否则你会重复10次相同的阵列,这可能不是你想要的。

I am not sure what you want to do with your iterations. Do note that Ruby arrays are 0-indexed, so a 10-element array will have an index in 0...9. Also note that iteration over an array is usually done with each, as Carpetsmoker notes in comments:

我不确定你想要用你的迭代做什么。请注意,Ruby数组是0索引的,因此10个元素的数组将具有0 ... 9的索引。另请注意,对于数组的迭代通常是通过每个进行的,正如Carpetsmoker在评论中所说:

grid.each do |row|
  row.each_index do |index|
    row[index] = "abcde"[rand(5)]
  end
end

EDIT: Thanks Cary!

编辑:谢谢卡里!

#2


1  

Your question is not clear. If you want to put the letter 'a' at n random locations in grid, you could do this:

你的问题不明确。如果你想把字母'a'放在网格中的n个随机位置,你可以这样做:

def salt(grid, obj, n)
  m = grid.size
  locs = (0...m*m).to_a.sample(n)
  locs.each do |l|
    row, col = l.divmod(m)
    grid[row][col] = obj
  end
end

grid = Array.new(10) { Array.new(10, ' ') }

salt(grid,'a',30)
grid
  #=> [[" ", "a", " ", " ", "a", "a", "a", " ", " ", " "],
  #    ["a", "a", " ", "a", "a", " ", " ", "a", " ", " "],
  #    [" ", "a", " ", " ", "a", " ", " ", " ", "a", " "],
  #    [" ", " ", " ", "a", " ", " ", "a", " ", " ", "a"],
  #    [" ", " ", " ", "a", " ", " ", " ", " ", " ", " "],
  #    [" ", " ", "a", " ", " ", " ", " ", " ", " ", " "],
  #    ["a", " ", " ", " ", " ", " ", "a", " ", " ", "a"],
  #    [" ", " ", "a", " ", " ", "a", " ", "a", " ", " "],
  #    [" ", "a", " ", " ", "a", " ", " ", " ", "a", "a"],
  #    [" ", " ", "a", "a", "a", " ", " ", " ", " ", " "]]

You could instead write:

你可以写:

locs =  n.times.map { rand(m*m) }

but that will likely result in some duplicates, in which case fewer than n cells will be filled with "a"'s. For example, when I computed locs that way for n=30 I found:

但这可能会导致一些重复,在这种情况下,少于n个单元格将填充“a”。例如,当我计算n = 30的locs时,我发现:

locs.uniq.size
  #=> 27