Having the array "a" I want to look for a pair of values within array "a". "02" followed by "10" and if found, I want to create 2 new arrays, one beginning in "02", "10" and the other beginning 3 positions after "02", "10".
a = ["11", "45", "01", "01", "02", "00", "10", "4C", "02", "10", "00", "42", "00", "00", "26"]
If I look the index of 02 and 10 individually I get 4 and 6 respectively, but index of the pair "02", "10" is 8.
irb(main)> a.index("02")
=> 4
irb(main)> a.index("10")
=> 6
The desired output would be like this:
b = ["02", "10", "00", "42", "00", "00", "26"]
c = ["00", "00", "26"]
How can I do this?
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