how to loop through two arrays in parallel

M

Mitchell Gould

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[]?
 
G

Glen Holcomb

[Note: parts of this message were removed to make it a legal post.]

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[]?
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.
 
S

Siep Korteling

Glen said:
Is there a way in Ruby to do this?


for product in products[] and quantity in quantity[]?
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
 
R

RKH

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[]?

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
 
T

ThoML

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.
 
R

Rick DeNatale

[Note: parts of this message were removed to make it a legal post.]

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[]?
%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"]
 
M

Mitchell Gould

Kevin said:
I'll start

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


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

Thanks Kevin.
 
M

Mitchell Gould

Rick said:
Is there a way in Ruby to do this?


for product in products[] and quantity in quantity[]?
%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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top