How to loop through two arrays in parallel

I have two arrays from a form.

params[:products] and params[:quantity]

which are products[] and quantity[]

I would like to extract the data from each item to get their name and
quantity.

Is there a way in Ruby to do this?

for product in products[] and quantity in quantity[]?

On Mon, Jul 14, 2008 at 2:03 PM, Mitchell G. [email protected]
wrote:

Is there a way in Ruby to do this?

for product in products[] and quantity in quantity[]?

Posted via http://www.ruby-forum.com/.

product_quantity = {}

products.each_with_index do |product, index|
product_quantity[product.to_sym] = quantity[index]
end

Not the prettiest but assuming the two arrays are one-to-one it should
give
you a hash with product names for keys and quantities as values.

“Hey brother Christian with your high and mighty errand, Your actions
speak
so loud, I can’t hear a word you’re saying.”

-Greg Graffin (Bad Religion)

I’ll start

for i in (0…products.size-1)
p products[i]
p quantity[i]
end

On 14 Jul., 22:03, Mitchell G. [email protected] wrote:

for product in products[] and quantity in quantity[]?

Posted viahttp://www.ruby-forum.com/.

What are you going to do with it. Probably I hash would be a better
idea…

products.each_with_index do |product,index|
puts “Quantity for #{product} is #{quantity[index]}” # or what
ever you want to do
end

Thanks Greg.

On Mon, Jul 14, 2008 at 4:03 PM, Mitchell G. [email protected]
wrote:

Is there a way in Ruby to do this?

for product in products[] and quantity in quantity[]?

Posted via http://www.ruby-forum.com/.

%w(a b c).zip([2, 4, 3]).map {|product, quantity| “#{quantity}
#{product} on
hand”}
=> [“2 a on hand”, “4 b on hand”, “3 c on hand”]


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

Glen H. wrote:

On Mon, Jul 14, 2008 at 2:03 PM, Mitchell G. [email protected]
wrote:

Is there a way in Ruby to do this?

for product in products[] and quantity in quantity[]?

Posted via http://www.ruby-forum.com/.

product_quantity = {}

products.each_with_index do |product, index|
product_quantity[product.to_sym] = quantity[index]
end

orderlist = products.zip(quantity)

results in an array of arrays in stead of a hash. A hash is faster for
lookup but looses the order (in Ruby 1.8.6), an array preserves the
order.

Hth,

Siep

Rick Denatale wrote:

On Mon, Jul 14, 2008 at 4:03 PM, Mitchell G. [email protected]
wrote:

Is there a way in Ruby to do this?

for product in products[] and quantity in quantity[]?

Posted via http://www.ruby-forum.com/.

%w(a b c).zip([2, 4, 3]).map {|product, quantity| “#{quantity}
#{product} on
hand”}
=> [“2 a on hand”, “4 b on hand”, “3 c on hand”]


Rick DeNatale

My blog on Ruby
http://talklikeaduck.denhaven2.com/

Rick I checked out your blog. Pretty interesting stuff. Over my head
most of it.
I liked the history of how you got into programming.
Thanks for your help.

orderlist = products.zip(quantity)

If you don’t want to create a new array but work on the values you
could also use an iterator/enumerator.

quant_enum = quantity.enum_for
products.each {|prod| do_something(prod, quant_enum.next)}

I don’t know though if this performs better/worse than #zip or
#each_with_index as proposed above/below.

Kevin B. wrote:

I’ll start

for i in (0…products.size-1)
p products[i]
p quantity[i]
end

I like this one. For a beginner this is easiest to follow.

Thanks Kevin.

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