[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

how to loop through two arrays in parallel

Mitchell Gould

7/14/2008 8:03:00 PM

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[]?
--
Posted via http://www.ruby-....

8 Answers

Glen Holcomb

7/14/2008 8:10:00 PM

0

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

On Mon, Jul 14, 2008 at 2:03 PM, Mitchell Gould <go_mitchell@yahoo.ca>
wrote:

> 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[]?
> --
> Posted via http://www.ruby-....
>
>
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)

Siep Korteling

7/14/2008 8:16:00 PM

0

Glen Holcomb wrote:
> On Mon, Jul 14, 2008 at 2:03 PM, Mitchell Gould <go_mitchell@yahoo.ca>
> wrote:
>
>> Is there a way in Ruby to do this?
>>
>>
>> for product in products[] and quantity in quantity[]?
>> --
>> Posted via http://www.ruby-....
>>
>>
> 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
--
Posted via http://www.ruby-....

Mitchell Gould

7/14/2008 8:16:00 PM

0



Thanks Greg.
--
Posted via http://www.ruby-....

konstantin.mailinglists

7/14/2008 8:17:00 PM

0

On 14 Jul., 22:03, Mitchell Gould <go_mitch...@yahoo.ca> wrote:
> 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[]?
> --
> Posted viahttp://www.ruby-....

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

ThoML

7/14/2008 8:35:00 PM

0

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

Rick DeNatale

7/14/2008 8:56:00 PM

0

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

On Mon, Jul 14, 2008 at 4:03 PM, Mitchell Gould <go_mitchell@yahoo.ca>
wrote:

> 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[]?
> --
> Posted via http://www.ruby-....
>
>
%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.denh...

Mitchell Gould

7/14/2008 9:05:00 PM

0

Kevin Brown 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.
--
Posted via http://www.ruby-....

Mitchell Gould

7/14/2008 9:13:00 PM

0

Rick Denatale wrote:
> On Mon, Jul 14, 2008 at 4:03 PM, Mitchell Gould <go_mitchell@yahoo.ca>
> wrote:
>
>> Is there a way in Ruby to do this?
>>
>>
>> for product in products[] and quantity in quantity[]?
>> --
>> Posted via http://www.ruby-....
>>
>>
> %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.denh...

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.
--
Posted via http://www.ruby-....