From: "ara.t.howard" Date: 2008-09-11T00:55:14+09:00 Subject: Re: How to operate on 2 arrays simultaneously? On Sep 10, 2008, at 9:42 AM, Patrick Doyle wrote: > There has got to be a more elegant solution than this. Suppose I have > 2 identical length arrays that I want to add together to produce a > third array. The best solution I've come up with is: > > c = (0...a.size).map {|i| a[i] + b[i]} > > Any suggestions? cfp:~ > cat a.rb a = 0,1,2 b = 3,4,5 c = a.zip(b).map{|pair| pair.first + pair.last} p c c = Array.new(a.size){|i| a[i] + b[i]} p c cfp:~ > ruby a.rb [3, 5, 7] [3, 5, 7] the second is far more efficient for large arrays - in terms of memory. a @ http://codeforpeople.com/ -- we can deny everything, except that we have the possibility of being better. simply reflect on that. h.h. the 14th dalai lama