Dividing inside iterations?

Hi

Learning R…

Is it possible to divide values while inside an iteration?

for example i’d like to do something like this:

h = [40,20,50,200]
h.each do |num,total| num / 24 = total
print “#{total} \n” end

as you’d expect, there is an error because of that ‘/’ and ‘=’ sign in
this iterator.

so since this isn’t possible what would be the best way to do what i’m
trying to do? or perhaps my syntax is wrong

thank you very much.

Dominic

Yes, but in assignement the variable goes on the left.
h = [40,20,50,200]
h.each do |num,total|
total = num / 24
print “#{total} \n”
end

j`ey
http://www.eachmapinject.com

Thanks for your quick reply Joey.

But it turns out you were wrong.

jk. it worked.

Thanks again man.

Dominic S.

Joey wrote:

Yes, but in assignement the variable goes on the left.
h = [40,20,50,200]
h.each do |num,total|
total = num / 24
print “#{total} \n”
end

j`ey
http://www.eachmapinject.com

Hi –

On Sun, 23 Jul 2006, Joey wrote:

Yes, but in assignement the variable goes on the left.
h = [40,20,50,200]
h.each do |num,total|

The “total” there doesn’t do anything; you’d just want:

h.each do |num|

total = num / 24
print “#{total} \n”

Of course this could also be:

puts num / 24

:slight_smile:

David

On 7/23/06, [email protected] [email protected] wrote:

Of course this could also be:

puts num / 24

Or:

h = [40,20,50,200]
h.map{|num|num/24}.each do |num|
puts num
end

j`ey
http://www.eachmapinject.com

Hi –

On Sun, 23 Jul 2006, Joey wrote:

h.map{|num|num/24}.each do |num|
puts num
end

Or:

puts [40,20,50,200].map {|num| num/24 }

David

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs