From: "Kroeger Simon (ext)" Date: 2005-09-06T18:15:56+09:00 Subject: Re: how to do insertion and smooth traversal simultaneously on array > From: William James [mailto:w_a_x_man@yahoo.com] > [...] > > If the array isn't flat: > ----------------------------------------- > a = [ [81,"&"], 82, 83, [84,"@"], 85 ] > a = (0...a.size).to_a.zip(a) > p a > a = a.inject([]) { |ary,x| > if x[0] % 2 == 0 # even index ? > ary += [ ":-)", x[1] ] > else > ary << x[1] > end > } > p a > ----------------------------------------- > [[0, [81, "&"]], [1, 82], [2, 83], [3, [84, "@"]], [4, 85]] > [":-)", [81, "&"], 82, ":-)", 83, [84, "@"], ":-)", 85] and another one: ----------------------------------------- a = [ [81,"&"], 82, 83, [84,"@"], 85 ] a = (0...a.size).to_a.zip(a) p a a.reverse_each do |i, v| if i % 2 == 0 # even index ? a.insert(i, [nil, ':-)']) end end p a.transpose[1] ----------------------------------------- [[0, [81, "&"]], [1, 82], [2, 83], [3, [84, "@"]], [4, 85]] [":-)", [81, "&"], 82, ":-)", 83, [84, "@"], ":-)", 85] cheers Simon