# Two arrays problem (although different from the other two ar

**URL:** <https://www.ruby-forum.com/t/two-arrays-problem-although-different-from-the-other-two-ar/56220>\
**Category:** Ruby\
**Created:** [March 29, 2006, 8:49am UTC](https://www.ruby-forum.com/t/two-arrays-problem-although-different-from-the-other-two-ar/56220 "2006-03-29T08:49:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kev\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/2bfe46/32.png) [@Kev\_J](https://www.ruby-forum.com/u/Kev_J)\
**Post date:** [March 29, 2006, 8:49am UTC](https://www.ruby-forum.com/t/two-arrays-problem-although-different-from-the-other-two-ar/56220/1 "2006-03-29T08:49:06Z")

</div>

I have a bunch of japanese encoded words in Kanji and Hirigana (I  
think). Each set is broken into two pieces, name and type, which can be  
combined to produce many more words, so this looks like a classic  
‘iterate over two arrays problem’

This is what I have, I tried messing with inject and I couldn’t get the  
desired output. If anyone wants to golf it I’d like to learn what I  
should be doing instead

corp\_data\_1.each do |cd1|  
corp\_data\_2.each do |cd2|  
rd \<\< cd1 + cd2  
end  
end

Thanks  
Kev

---

<div class="post-metadata">

**Author:** ![Kev\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/2bfe46/32.png) [@Kev\_J](https://www.ruby-forum.com/u/Kev_J)\
**Post date:** [March 29, 2006, 10:31am UTC](https://www.ruby-forum.com/t/two-arrays-problem-although-different-from-the-other-two-ar/56220/2 "2006-03-29T10:31:07Z")

</div>

Kev J. wrote:

> ```
> corp_data_2.each do |cd2|
> rd << cd1 + cd2
> end
> end
> 
> ```
> 
> Thanks  
> Kev

Not necessarily better but:

rd = corp\_data\_1.map{|cd1| corp\_data\_2.map{|cd2| cd1 + cd2}}

cheers

---

<div class="post-metadata">

**Author:** ![Kev\_J](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/k/2bfe46/32.png) [@Kev\_J](https://www.ruby-forum.com/u/Kev_J)\
**Post date:** [March 29, 2006, 6:04pm UTC](https://www.ruby-forum.com/t/two-arrays-problem-although-different-from-the-other-two-ar/56220/3 "2006-03-29T18:04:46Z")

</div>

I’m not really clear what you’re trying to do. Could you provide a bit  
more detail? Do the elements of corp\_data1 and corp\_data2 correspond to  
one another or are they independent? Are you trying to get output with  
the same number of elements, or with a number of elements equal to the  
product of the two (a sort of cartesian product)?

If it’s the former, you should be able to do it with zip and map, if  
it’s the latter, it seems like what you have should be appropriate.
