Robert Klemme
1/23/2007 3:21:00 PM
On 23.01.2007 16:05, WKC CCC wrote:
> Does anyone know of a function that multiplies the contents of an array.
> For example:
>
> one = [1,2,3]
> two = [[2],[3],[4]]
>
> output = [[2],[6],[12]]
>
> I've written a simple function that does this, however I'm sure there is
> a better way, instead of casting the item to a float.
Why do you cast to float? You can multiply integers directly.
First with a straightforward array "two":
irb(main):001:0> require 'enumerator'
=> true
irb(main):002:0> one = [1,2,3]
=> [1, 2, 3]
irb(main):003:0> two = [2,3,4]
=> [2, 3, 4]
irb(main):004:0> one.to_enum(:zip, two).map {|a,b| a*b}
=> [2, 6, 12]
Now with your array:
irb(main):005:0> two.map! {|i| [i]}
=> [[2], [3], [4]]
irb(main):006:0> one.to_enum(:zip, two).map {|a,b| a * b[0]}
=> [2, 6, 12]
Kind regards
robert