可以说我有一个数组,像这样: ['x','cat', 'dog', 'x', 'dolphin', 'cougar', 'whale']
切片数组元素时达到
我不知道数组或当“X”会发生的长度。当我到达'x'时,我想将下列元素推入新的数组中,直到我到达下一个元素includes?('x')
。
所需的输出将是: [['cat', 'dog']['dolphin','cougar', 'whale']]
我怎样才能做到这一点?
可以说我有一个数组,像这样: ['x','cat', 'dog', 'x', 'dolphin', 'cougar', 'whale']
切片数组元素时达到
我不知道数组或当“X”会发生的长度。当我到达'x'时,我想将下列元素推入新的数组中,直到我到达下一个元素includes?('x')
。
所需的输出将是: [['cat', 'dog']['dolphin','cougar', 'whale']]
我怎样才能做到这一点?
好老Enumerable#reduce
是很方便的事情太多了:
def split_array_by_item(array, item)
array.reduce([]) do |memo, x|
memo.push([]) if (x == item) || memo.empty?
memo[-1].push(x) unless x == item
memo
end
end
a = ['x', 'cat', 'dog', 'x', 'dolphin', 'cougar', 'whale']
split_array_by_item(a, 'x') # => [["cat", "dog"], ["dolphin", "cougar", "whale"]]
[编辑]另外:
def split_array_by_item(array, item)
array.chunk{|x|x==item}.reject(&:first).map(&:last)
end
ar = ['x', 'cat', 'dog', 'x', 'dolphin', 'cougar', 'whale']
p ar.chunk{|el| el == 'x'}.each_slice(2).map{|el| el.last.last}
#=> [["cat", "dog"], ["dolphin", "cougar", "whale"]]
大部分的工作是斩去的不需要的侧面结果方法chunk
。
Enumerable#chunk
是要走的路。您可以使用nil
放弃那些块你不想:
arr = ['x','cat', 'dog', 'x', 'dolphin', 'cougar', 'whale']
arr.chunk{ |e| e != 'x' || nil }.map(&:last)
#=> [["cat", "dog"], ["dolphin", "cougar", "whale"]]
酷。 :'arr.chunk {| e | true if e!='x'} .map(&:last)''你可以用任何真理替换'true'。 – 2014-09-26 04:48:01
这个版本非常明确:'arr。 chunk {| e | e =='x'?:_separator::payload} .map(&:last)' – 2014-09-26 04:50:12
Enumerable#slice_before
使这个简单的:
a = ['x','cat', 'dog', 'x', 'dolphin', 'cougar', 'whale']
a.slice_before(/\Ax\z/).map { |chunk| chunk.drop(1) }
=> [["cat", "dog"], ["dolphin", "cougar", "whale"]]
+1,我希望有人会提到'slice_before'。 – 2013-05-10 04:34:22
由于红宝石2.0,一个很好的解决方案是slice_before方法或自2.2 slice_when方法:
但是我们需要降每个阵列产生的第一个元素'x':
ary = ['x', 'cat', 'dog', 'x', 'dolphin', 'cougar', 'whale']
ary.slice_before{|e| e=='x'}.map{|t| t.drop(1)}
#==> [["cat", "dog"], ["dolphin", "cougar", "whale"]]
ary.slice_when{|i,j| j=='x'}.map{|t| t.drop(1)}
#==> [["cat", "dog"], ["dolphin", "cougar", "whale"]]
啊,亲爱的SO'你到目前为止试过了什么?'。 试了很多,但它没有让我远...不知道如何解决这个问题。 IM STUCK :( – Snarf 2013-05-09 22:39:05
如果一行中有两个“x”或结尾是“x”,该怎么办?有什么行为?忽略或者有一个空的'[]'? – 2013-05-10 14:08:38