Ruby 对于具有递增数组的每个循环
我试图在ruby中使用for-each循环遍历数组,但在循环内部,我有条件地增加数组的大小。我希望遍历数组,直到对数组中的每个元素(包括我添加的所有元素)运行了迭代Ruby 对于具有递增数组的每个循环,ruby,arrays,foreach,Ruby,Arrays,Foreach,我试图在ruby中使用for-each循环遍历数组,但在循环内部,我有条件地增加数组的大小。我希望遍历数组,直到对数组中的每个元素(包括我添加的所有元素)运行了迭代 for x in fol t = get_transition(x,"") for i in t if i != nil && !fol.include?(i) fol = fol.push(i) fol = fol.flatten
for x in fol
t = get_transition(x,"")
for i in t
if i != nil && !fol.include?(i)
fol = fol.push(i)
fol = fol.flatten
end
end
end
在该代码的第一个循环中,数组
fol = [1]
它将元素3添加到数组中
fol = [1, 3]
然后,它将以x=3再次运行循环,数组变为
fol = [1, 3, 2]
但它不会在x=2时再次迭代。
提前感谢您的帮助
为了澄清起见,我添加了打印语句及其生成的输出
fol.each do |x|
puts "fol = #{fol}"
puts "x = #{x}"
t = get_transition(x,"")
puts "t = #{t}"
t.each do |i|
puts "i = #{i}"
if i != nil && !fol.include?(i)
fol = fol.push(i)
fol = fol.flatten
end
end
end
puts "\nfol = #{fol}"
此代码生成此输出
fol = [1]
x = 1
t = [3]
i = 3
fol = [1, 3]
x = 3
t = [2]
i = 2
fol = [1, 3, 2]
我试图在ruby中使用for-each循环遍历数组
但是在循环内部,我增加了数组的大小
有条件地。我希望遍历数组,直到运行完为止
迭代数组中的每个元素,包括
增加
for x in fol
t = get_transition(x,"")
for i in t
if i != nil && !fol.include?(i)
fol = fol.push(i)
fol = fol.flatten
end
end
end
为什么不把它当作一个队列呢?这和你描述的差不多
queue = fol.clone
until queue.empty?
x = queue.pop
t = get_transition(x,"")
for i in t
if i != nil && !fol.include?(i)
# Not too sure what type "i" is here, but you push onto the queue here
# I'd try to avoid flattening if you know what your data types are as it will be slow
end
end
end
我试图在ruby中使用for-each循环遍历数组
但是在循环内部,我增加了数组的大小
有条件地。我希望遍历数组,直到运行完为止
迭代数组中的每个元素,包括
增加
for x in fol
t = get_transition(x,"")
for i in t
if i != nil && !fol.include?(i)
fol = fol.push(i)
fol = fol.flatten
end
end
end
为什么不把它当作一个队列呢?这和你描述的差不多
queue = fol.clone
until queue.empty?
x = queue.pop
t = get_transition(x,"")
for i in t
if i != nil && !fol.include?(i)
# Not too sure what type "i" is here, but you push onto the queue here
# I'd try to avoid flattening if you know what your data types are as it will be slow
end
end
end
我试图在ruby中使用for-each循环遍历数组
但是在循环内部,我增加了数组的大小
有条件地。我希望遍历数组,直到运行完为止
迭代数组中的每个元素,包括
增加
for x in fol
t = get_transition(x,"")
for i in t
if i != nil && !fol.include?(i)
fol = fol.push(i)
fol = fol.flatten
end
end
end
为什么不把它当作一个队列呢?这和你描述的差不多
queue = fol.clone
until queue.empty?
x = queue.pop
t = get_transition(x,"")
for i in t
if i != nil && !fol.include?(i)
# Not too sure what type "i" is here, but you push onto the queue here
# I'd try to avoid flattening if you know what your data types are as it will be slow
end
end
end
我试图在ruby中使用for-each循环遍历数组
但是在循环内部,我增加了数组的大小
有条件地。我希望遍历数组,直到运行完为止
迭代数组中的每个元素,包括
增加
for x in fol
t = get_transition(x,"")
for i in t
if i != nil && !fol.include?(i)
fol = fol.push(i)
fol = fol.flatten
end
end
end
为什么不把它当作一个队列呢?这和你描述的差不多
queue = fol.clone
until queue.empty?
x = queue.pop
t = get_transition(x,"")
for i in t
if i != nil && !fol.include?(i)
# Not too sure what type "i" is here, but you push onto the queue here
# I'd try to avoid flattening if you know what your data types are as it will be slow
end
end
end
当
x=3
时,它是如何从[1,3]
到[1,3,2]
的?您所说的“x=2时不会再次迭代”是什么意思?如果您可以(编辑以)给出一个get_transition
示例,以及相关的所需输出,这将非常有用。@CarySwoveland我认为2是通过fol.push(I)
在x=3
迭代中添加的。问题是它不会继续进行x=2
迭代。我应该补充一点,这段代码非常混乱,尤其是在循环中重新分配fol
的部分。它需要完全重写才能更地道,但我无法从上下文中判断您要做什么。是的,get_转换(3“”)返回2,然后将其添加到数组中,这是fol
的含义?你想做什么?你想用它做什么?可能有一种更简洁、更简洁的方法。当x=3
时,如何从[1,3]
到[1,3,2]
?您所说的“x=2时不会再次迭代”是什么意思?如果您可以(编辑以)给出一个get_transition
示例,以及相关的所需输出,这将非常有用。@CarySwoveland我认为2是通过fol.push(I)
在x=3
迭代中添加的。问题是它不会继续进行x=2
迭代。我应该补充一点,这段代码非常混乱,尤其是在循环中重新分配fol
的部分。它需要完全重写才能更地道,但我无法从上下文中判断您要做什么。是的,get_转换(3“”)返回2,然后将其添加到数组中,这是fol
的含义?你想做什么?你想用它做什么?可能有一种更简洁、更简洁的方法。当x=3
时,如何从[1,3]
到[1,3,2]
?您所说的“x=2时不会再次迭代”是什么意思?如果您可以(编辑以)给出一个get_transition
示例,以及相关的所需输出,这将非常有用。@CarySwoveland我认为2是通过fol.push(I)
在x=3
迭代中添加的。问题是它不会继续进行x=2
迭代。我应该补充一点,这段代码非常混乱,尤其是在循环中重新分配fol
的部分。它需要完全重写才能更地道,但我无法从上下文中判断您要做什么。是的,get_转换(3“”)返回2,然后将其添加到数组中,这是fol
的含义?你想做什么?你想用它做什么?可能有一种更简洁、更简洁的方法。当x=3
时,如何从[1,3]
到[1,3,2]
?您所说的“x=2时不会再次迭代”是什么意思?如果您可以(编辑以)给出一个get_transition
示例,以及相关的所需输出,这将非常有用。@CarySwoveland我认为2是通过fol.push(I)
在x=3
迭代中添加的。问题是它不会继续进行x=2
迭代。我应该补充一点,这段代码非常混乱,尤其是在循环中重新分配fol
的部分。它需要完全重写才能更地道,但我无法从上下文中判断您要做什么。是的,get_转换(3“”)返回2,然后将其添加到数组中,这是fol
的含义?你想做什么?你想用它做什么?可能有一个更干净、更短的方法来做。谢谢你,这非常有效。这正是我所需要的谢谢你这工作做得很好。正是我所需要的