在值对之前剪切数组元素
Cut array elements before pair of values
有了数组 "a" 我想在数组 "a" 中查找一对值。 “02”后跟“10”,如果找到,我想创建 2 个新数组,一个
从“02”、“10”和“02”、“10”之后的其他 3 个位置开始。
a = ["11", "45", "01", "01", "02", "00", "10", "4C", "02", "10", "00", "42", "00", "00", "26"]
如果我分别查看 02 和 10 的索引,我分别得到 4 和 6,但是“02”、“10”对的索引是 8。
irb(main)> a.index("02")
=> 4
irb(main)> a.index("10")
=> 6
期望的输出是这样的:
b = ["02", "10", "00", "42", "00", "00", "26"]
c = ["00", "00", "26"]
我该怎么做?
def find_em(a)
i = (a.size-1).times.find { |i| [a[i], a[i+1]] == ["02", "10"] }
i.nil? ? nil : [a[i..-1], a[i+4..-1] || []]
end
find_em(a)
#=> [["02", "10", "00", "42", "00", "00", "26"], ["00", "00", "26"]]
find_em ["10", "4C", "02", "10", "00", "42"]
#=> [["02", "10", "00", "42"], []]
find_em ["10", "4C", "02", "10"]
#=> [["02", "10"], []]
find_em ["10", "4C", "10", "00", "42"]
#=> nil
find_em []
#=> nil
为了让它与更多的 ["02", "10"]
一起工作,有条件地从@Cary Swoveland 的回答中窃取:)
def split_strange(ary)
res = []
(ary.size - 1).times do |i|
res << [ary[i..-1], ary[i+4..-1]] if [ary[i], ary[i+1]] == ["02", "10"]
end
return res.flatten(1) # or whathever
end
因此,它还可以处理:
k = ["02", "10", "02", "10", "00", "42", "00", "00", "26"]
split_strange(k)
#=> [["02", "10", "02", "10", "00", "42", "00", "00", "26"], ["00", "42", "00", "00", "26"], ["02", "10", "00", "42", "00", "00", "26"], ["00", "00", "26"]]
替代方案,但只适用于一次:
def split_strange_2(ary)
tmp = ary.slice_when { |a, b| a == "02" and b == "10" }.to_a
res = tmp.last.unshift tmp.first.last
return res, res[4..-1] || [] if tmp.size == 2
end
split_strange_2(a)
#=> [["02", "10", "00", "42", "00", "00", "26"], ["00", "00", "26"]]
有了数组 "a" 我想在数组 "a" 中查找一对值。 “02”后跟“10”,如果找到,我想创建 2 个新数组,一个 从“02”、“10”和“02”、“10”之后的其他 3 个位置开始。
a = ["11", "45", "01", "01", "02", "00", "10", "4C", "02", "10", "00", "42", "00", "00", "26"]
如果我分别查看 02 和 10 的索引,我分别得到 4 和 6,但是“02”、“10”对的索引是 8。
irb(main)> a.index("02")
=> 4
irb(main)> a.index("10")
=> 6
期望的输出是这样的:
b = ["02", "10", "00", "42", "00", "00", "26"]
c = ["00", "00", "26"]
我该怎么做?
def find_em(a)
i = (a.size-1).times.find { |i| [a[i], a[i+1]] == ["02", "10"] }
i.nil? ? nil : [a[i..-1], a[i+4..-1] || []]
end
find_em(a)
#=> [["02", "10", "00", "42", "00", "00", "26"], ["00", "00", "26"]]
find_em ["10", "4C", "02", "10", "00", "42"]
#=> [["02", "10", "00", "42"], []]
find_em ["10", "4C", "02", "10"]
#=> [["02", "10"], []]
find_em ["10", "4C", "10", "00", "42"]
#=> nil
find_em []
#=> nil
为了让它与更多的 ["02", "10"]
一起工作,有条件地从@Cary Swoveland 的回答中窃取:)
def split_strange(ary)
res = []
(ary.size - 1).times do |i|
res << [ary[i..-1], ary[i+4..-1]] if [ary[i], ary[i+1]] == ["02", "10"]
end
return res.flatten(1) # or whathever
end
因此,它还可以处理:
k = ["02", "10", "02", "10", "00", "42", "00", "00", "26"]
split_strange(k)
#=> [["02", "10", "02", "10", "00", "42", "00", "00", "26"], ["00", "42", "00", "00", "26"], ["02", "10", "00", "42", "00", "00", "26"], ["00", "00", "26"]]
替代方案,但只适用于一次:
def split_strange_2(ary)
tmp = ary.slice_when { |a, b| a == "02" and b == "10" }.to_a
res = tmp.last.unshift tmp.first.last
return res, res[4..-1] || [] if tmp.size == 2
end
split_strange_2(a)
#=> [["02", "10", "00", "42", "00", "00", "26"], ["00", "00", "26"]]