4

I have 0.0 24.0 0.0 12.0 0.0 0.0 0.0 0.0 0.0 how do i get the sum of these values? .sum etc do not work on floats

EDIT:

Im doing

<% @data.each do |data| %>
    <%= data[ :values ]%>
<%end%>

Where data[:values] prints [5.0, 0.0, 0.0, 0.0, 0.0] [4.0, 0.0, 0.0, 0.0, 0.0] [1.0, 0.0, 0.0, 0.0, 0.0] and i only want to get the first value of each array and sum them together to get 10.0

@data prints

[{:name=>"BMW", :values=>[0.0, 0.0, 0.0, 0.0, 0.0]}, {:name=>"Asda", :values=>[32.0, 12.0, 0.0, 0.0, 0.0]}]
ahmet
  • 4,955
  • 11
  • 39
  • 64
  • possible duplicate of [How to sum array members in Ruby?](http://stackoverflow.com/questions/1538789/how-to-sum-array-members-in-ruby) ... sorry, only realized that after answering. – derobert Oct 26 '11 at 14:18
  • 2
    could you post a sample `@data` content? and how do you use `inject` – Bohdan Oct 26 '11 at 14:40

4 Answers4

8

Assuming they are in an array, this works:

irb(main):001:0> [0.0, 1.0, 3.0].inject(:+)
=> 4.0

Edit: from your edited question, it appears you want:

@data.reduce(0) { |sum, x| sum += x[:values][0] }

Which grabs the first (0th) element of each :values, and sums them all together:

irb(main):003:0> @data
=> [{:values=>[0.0, 0.0, 0.0, 0.0, 0.0], :name=>"BMW"}, {:values=>[32.0, 12.0, 0.0, 0.0, 0.0], :name=>"Asda"}]
irb(main):004:0> @data.reduce(0) { |sum, x| sum += x[:values][0] }
=> 32.0
derobert
  • 49,731
  • 15
  • 94
  • 124
3

Here:

irb(main):001:0> values = [[1.0,0.0],[2.0,0.0],[3.0,0.0]]
=> [[1.0, 0.0], [2.0, 0.0], [3.0, 0.0]]
irb(main):002:0> values.map(&:first)
=> [1.0, 2.0, 3.0]
irb(main):003:0> values.map(&:first).inject(:+)
=> 6.0

Revised:

@data.map {|hash| hash[:values].first}.inject(:+)
Ryanmt
  • 3,215
  • 3
  • 22
  • 23
  • undefined method `first' for 0.0:Float i dont know why its saying this? – ahmet Oct 26 '11 at 14:34
  • [0.0, 0.0, 0.0, 0.0, 0.0] [23.0, 24.0, 0.0, 0.0, 0.0] [0.0, 0.0, 0.0, 0.0, 0.0] [32.0, 12.0, 0.0, 0.0, 0.0] [0.0, 0.0, 0.0, 0.0, 0.0] [123.0, 0.0, 0.0, 0.0, 0.0] [0.0, 0.0, 0.0, 0.0, 0.0] [0.0, 0.0, 0.0, 0.0, 0.0] [0.0, 0.0, 0.0, 0.0, 0.0] Is what is getting printed out – ahmet Oct 26 '11 at 14:37
  • Your code shows that you are using an array called `@data`. Try this on that object, as in `@data.map(&:first).inject(:+)`. Also, tell me what `@data.class` tells you. – Ryanmt Oct 26 '11 at 14:40
  • .class says Array, I've editted my question with the outputs of @data (&:first) gets the name which i do not want – ahmet Oct 26 '11 at 14:44
  • 1
    Wow... Okay, so you first will need to extract the values. `@data.map {|hash| hash[:values].first}.inject(:+)` However, from what I'm seeing, I don't think you really want just the first value from your `values` array. – Ryanmt Oct 26 '11 at 14:52
-1

If you have array of them:

[0.0, 24.0, 0.0, 12.0, 0.0, 0.0, 0.0, 0.0, 0.0].reduce(:+)

If not - use +.

0.0 + 24.0 + 0.0 + 12.0 + 0.0 + 0.0 + 0.0 + 0.0 + 0.0

=> 36.0 Or i miss something?

Sigurd
  • 7,865
  • 3
  • 24
  • 34
-1

Do you need to get the sum in some fancy way, or is it okay to do it in a loop?

I would do it like the following:

sum = 0
array.each do |a|
  sum+=a
end
Allan Nørgaard
  • 284
  • 2
  • 11