2013-05-09 84 views
3

可以说我有一个数组,像这样: ['x','cat', 'dog', 'x', 'dolphin', 'cougar', 'whale']切片数组元素时达到

我不知道数组或当“X”会发生的长度。当我到达'x'时,我想将下列元素推入新的数组中,直到我到达下一个元素includes?('x')

所需的输出将是: [['cat', 'dog']['dolphin','cougar', 'whale']]

我怎样才能做到这一点?

+2

啊,亲爱的SO'你到目前为止试过了什么?'。 试了很多,但它没有让我远...不知道如何解决这个问题。 IM STUCK :( – Snarf 2013-05-09 22:39:05

+0

如果一行中有两个“x”或结尾是“x”,该怎么办?有什么行为?忽略或者有一个空的'[]'? – 2013-05-10 14:08:38

回答

2

好老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 
+0

先生,我欠你一杯啤酒。 – Snarf 2013-05-09 22:53:05

+3

@Snarf:我接受你的报价! – maerics 2013-05-09 22:53:18

+2

@Snarf我回答了,我也可以有吗? – squiguy 2013-05-09 22:54:22

3
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

3

Enumerable#chunk是要走的路。您可以使用nil放弃那些块你不想:

arr = ['x','cat', 'dog', 'x', 'dolphin', 'cougar', 'whale'] 

arr.chunk{ |e| e != 'x' || nil }.map(&:last) 
#=> [["cat", "dog"], ["dolphin", "cougar", "whale"]] 
+0

酷。 :'arr.chunk {| e | true if e!='x'} .map(&:last)''你可以用任何真理替换'true'。 – 2014-09-26 04:48:01

+0

这个版本非常明确:'arr。 chunk {| e | e =='x'?:_separator::payload} .map(&:last)' – 2014-09-26 04:50:12

7

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"]] 
+0

+1,我希望有人会提到'slice_before'。 – 2013-05-10 04:34:22

0

由于红宝石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"]]