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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.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 有条件地将数据插入数组_Ruby_Arrays - Fatal编程技术网

Ruby 有条件地将数据插入数组

Ruby 有条件地将数据插入数组,ruby,arrays,Ruby,Arrays,好的,有人能帮助我如何正确迭代动态大小数组吗? 我的意思是: my_array = [1, 2, 3, 4, 2, 5, 15, 2] # <= there are three 2 inside my_array 第一个方法不好,因为它在最后2个之后没有显示“good”,我猜这是因为迭代无法达到最后一个整数2(在最后一个数组中),这是预期的,因为每次插入“good”时数组大小都会变大 第二个也是坏的,因为我用“good”字符串替换每2个之后的数据。 现在,有人能告诉我,我怎样才能正确地

好的,有人能帮助我如何正确迭代动态大小数组吗? 我的意思是:

my_array = [1, 2, 3, 4, 2, 5, 15, 2] # <= there are three 2 inside my_array 
第一个方法不好,因为它在最后2个之后没有显示“good”,我猜这是因为迭代无法达到最后一个整数2(在最后一个数组中),这是预期的,因为每次插入“good”时数组大小都会变大

第二个也是坏的,因为我用“good”字符串替换每2个之后的数据。 现在,有人能告诉我,我怎样才能正确地完成这项工作,这样我才能制作出这样的作品:

 p my_array # => [1, 2, "good", 3, 4, 2, "good", 5, 15, 2, "good"]
添加所有“良好”而不替换任何数据


非常感谢您的帮助。

将其转换为新阵列比就地修改更好:

my_array = [1, 2, 3, 4, 2, 5, 15, 2]

def add_good(a)
  a.flat_map do |value|
    case (value)
    when 2
      [ 2, 'good' ]
    else
      value
    end
  end
end

puts add_good(my_array).inspect
# => [1, 2, "good", 3, 4, 2, "good", 5, 15, 2, "good"]
如果要在结果数组中创建零个或多个条目,则
flat_map
方法非常有用<代码>贴图是1:1贴图,
平面贴图
是1:N贴图

也可以使其更加通用:

def fancy_insert(a, insert)
  a.flat_map do |value|
    if (yield(value))
      [ value, insert ]
    else
      value
    end
  end
end

result = fancy_insert(my_array, 'good') do |value|
  value == 2
end

puts result.inspect

这样,您可以传入任意块和要插入的值。

将其转换为新数组比就地修改更好:

my_array = [1, 2, 3, 4, 2, 5, 15, 2]

def add_good(a)
  a.flat_map do |value|
    case (value)
    when 2
      [ 2, 'good' ]
    else
      value
    end
  end
end

puts add_good(my_array).inspect
# => [1, 2, "good", 3, 4, 2, "good", 5, 15, 2, "good"]
如果要在结果数组中创建零个或多个条目,则
flat_map
方法非常有用<代码>贴图是1:1贴图,
平面贴图
是1:N贴图

也可以使其更加通用:

def fancy_insert(a, insert)
  a.flat_map do |value|
    if (yield(value))
      [ value, insert ]
    else
      value
    end
  end
end

result = fancy_insert(my_array, 'good') do |value|
  value == 2
end

puts result.inspect

这样,您可以传入任意块和要插入的值。

为什么不使用
。每个带索引的方法:

arr = [1, 2, 3, 4, 5, 2, 3, 5]
arr.each_with_index {|e, i| arr.insert(i+1, "good") if e == 2}

快速而激烈。

为什么不使用
。每个带有索引的方法:

arr = [1, 2, 3, 4, 5, 2, 3, 5]
arr.each_with_index {|e, i| arr.insert(i+1, "good") if e == 2}

快速而激烈。

这里有另一种方法,使用枚举器:

my_array = [1, 2, 3, 4, 2, 5, 15, 2]

enum = my_array.each
  #=> #<Enumerator: [1, 2, 3, 4, 2, 5, 15, 2]:each>

my_array = []
loop do
  x = enum.next
  my_array << x
  my_array << "good" if x == 2
end
my_array
  #=> [1, 2, "good", 3, 4, 2, "good", 5, 15, 2, "good"]
my_数组=[1,2,3,4,2,5,15,2]
enum=my_array.each
#=> #
my_数组=[]
环道
x=enum.next

my_array这里有另一种方法,使用枚举器:

my_array = [1, 2, 3, 4, 2, 5, 15, 2]

enum = my_array.each
  #=> #<Enumerator: [1, 2, 3, 4, 2, 5, 15, 2]:each>

my_array = []
loop do
  x = enum.next
  my_array << x
  my_array << "good" if x == 2
end
my_array
  #=> [1, 2, "good", 3, 4, 2, "good", 5, 15, 2, "good"]
my_数组=[1,2,3,4,2,5,15,2]
enum=my_array.each
#=> #
my_数组=[]
环道
x=enum.next

我的_数组用于教育示例,我试图使逻辑尽可能清晰。三元运算符可能会有点混乱,而且当你想添加额外的情况时,也不能很好地“缩放”。这很公平。尽管如此,在您的
案例
语句的
else
分支中仍然不需要创建额外的数组。谢谢大家,我可以看到flat_-map方法非常好,我现在肯定使用flat_-map,再次感谢。对于教育示例,我尝试尽可能清晰地说明逻辑。三元运算符可能会有点混乱,而且当你想添加额外的情况时,也不能很好地“缩放”。这很公平。不过,在您的
案例
语句的
else
分支中,没有必要创建额外的数组。谢谢大家,我可以看到平面映射方法非常好,我现在肯定使用平面映射,再次感谢。哇,谢谢大家,它帮助我解决了我的问题。非常感谢=)不客气,别忘了将其标记为解决方案,以便其他人可以使用:)哇,谢谢,这有助于我解决问题。非常感谢=)不客气,别忘了将其标记为解决方案,以便其他人使用:)