# 2 Arrays into 1 Array with all possible combinations?

**URL:** https://rubytalk.org/t/2-arrays-into-1-array-with-all-possible-combinations/68895
**Category:** ruby-talk
**Created:** [19 December 2013 01:37 UTC](https://rubytalk.org/t/2-arrays-into-1-array-with-all-possible-combinations/68895 "2013-12-19T01:37:58Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)
#### Post date: [19 December 2013 01:37 UTC](https://rubytalk.org/t/2-arrays-into-1-array-with-all-possible-combinations/68895/1 "2013-12-19T01:37:58Z")

</div>

I have 2 arrays like so:

arr1 = [a, b, c]  
arr2 = 1, 2, 3]

I want to get:

[a1, a2, a3, b1, b2, b3, c1, c2, c3]

I've tried a few things like

output = arr1.each do |a1|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arr2.each do |a2|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

puts output

But that only lists the first array.

This works:  
output = arr1.each do |a1|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;arr2.each do |a2|  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts a1 + a2  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end

But my "output" variable isn't good. I get the desired text in the  
console, but I can't reuse it for what I really want to do.

This seems trivial, but I feel stupid. I can't even search for this  
properly. Please help... thanks.

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

### Author: ![Ammar\_Ali](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@Ammar\_Ali](https://rubytalk.org/u/Ammar_Ali)
#### Post date: [19 December 2013 01:57 UTC](https://rubytalk.org/t/2-arrays-into-1-array-with-all-possible-combinations/68895/2 "2013-12-19T01:57:32Z")

</div>

> I have 2 arrays like so:
> 
> arr1 = [a, b, c]  
> arr2 = 1, 2, 3]
> 
> I want to get:
> 
> [a1, a2, a3, b1, b2, b3, c1, c2, c3]

Here’s one way:

> > a1 = %w{a b c}

=\> ["a", "b", "c"]

> > a2 = %w{1 2 3}

=\> ["1", "2", "3"]

> > a1.map {|c1| a2.map {|c2| c1 + c2} }.flatten

=\> ["a1", "a2", "a3", "b1", "b2", "b3", "c1", "c2", "c3”]

HTH,  
Ammar

> **···**
>
> On Dec 19, 2013, at 3:37 AM, Mike Lerner \<lists@ruby-forum.com\> wrote:

---

<div class="post-metadata">

### Author: ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)
#### Post date: [20 December 2013 11:27 UTC](https://rubytalk.org/t/2-arrays-into-1-array-with-all-possible-combinations/68895/3 "2013-12-20T11:27:58Z")

</div>

ar1=['a','b','c']  
ar2=[1,2,3]  
ar3=Array.new()

ar1.each do |a| ar2.each do |n| ar3.push(a+n.to\_s) end end  
p ar3

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

### Author: ![abinoam](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/abinoam/32/1887_2.png) [@abinoam](https://rubytalk.org/u/abinoam)
#### Post date: [19 December 2013 02:09 UTC](https://rubytalk.org/t/2-arrays-into-1-array-with-all-possible-combinations/68895/4 "2013-12-19T02:09:37Z")

</div>

a1 = ["a", "b", "c"]  
a2 = ["1", "2", "3"]

a1.product(a2)  
# =\> [["a", "1"], ["a", "2"], ["a", "3"], ["b", "1"], ["b", "2"],  
["b", "3"], ["c", "1"], ["c", "2"], ["c", "3"]]

a1.product(a2).map {|a| a.join }  
# =\> ["a1", "a2", "a3", "b1", "b2", "b3", "c1", "c2", "c3"]

a1.product(a2).map(&:join)  
# =\> ["a1", "a2", "a3", "b1", "b2", "b3", "c1", "c2", "c3"]  
# same as above

# If you want to different things with one element  
a1.product(a2).map {|el1, el2| el1.upcase + el2 }  
=\> ["A1", "A2", "A3", "B1", "B2", "B3", "C1", "C2", "C3"]

Best regards,  
Abinoam Jr.

> **···**
>
> On Wed, Dec 18, 2013 at 10:57 PM, Ammar Ali \<ammarabuali@gmail.com\> wrote:
> 
> > On Dec 19, 2013, at 3:37 AM, Mike Lerner \<lists@ruby-forum.com\> wrote:
> > 
> > > I have 2 arrays like so:
> > > 
> > > arr1 = [a, b, c]  
> > > arr2 = 1, 2, 3]
> > > 
> > > I want to get:
> > > 
> > > [a1, a2, a3, b1, b2, b3, c1, c2, c3]
> > 
> > Here’s one way:
> > 
> > > > a1 = %w{a b c}
> > 
> > =\> ["a", "b", "c"]
> > 
> > > > a2 = %w{1 2 3}
> > 
> > =\> ["1", "2", "3"]
> > 
> > > > a1.map {|c1| a2.map {|c2| c1 + c2} }.flatten
> > 
> > =\> ["a1", "a2", "a3", "b1", "b2", "b3", "c1", "c2", "c3”]
> > 
> > HTH,  
> > Ammar

---

<div class="post-metadata">

### Author: ![7stud2](https://avatars.discourse-cdn.com/v4/letter/7/9de053/32.png) [@7stud2](https://rubytalk.org/u/7stud2)
#### Post date: [19 December 2013 02:11 UTC](https://rubytalk.org/t/2-arrays-into-1-array-with-all-possible-combinations/68895/5 "2013-12-19T02:11:24Z")

</div>

> > > a1 = %w{a b c}
> 
> =\> ["a", "b", "c"]
> 
> > > a2 = %w{1 2 3}
> 
> =\> ["1", "2", "3"]
> 
> > > a1.map {|c1| a2.map {|c2| c1 + c2} }.flatten
> 
> =\> ["a1", "a2", "a3", "b1", "b2", "b3", "c1", "c2", "c3]

That's it! Thank you!

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![stomar](https://avatars.discourse-cdn.com/v4/letter/s/41988e/32.png) [@stomar](https://rubytalk.org/u/stomar)
#### Post date: [19 December 2013 05:31 UTC](https://rubytalk.org/t/2-arrays-into-1-array-with-all-possible-combinations/68895/6 "2013-12-19T05:31:14Z")

</div>

@Ammar: "\>\>" is rendered as quote by many email clients,  
so using it as prompt is not so good in a mailing list.

Regards,  
Marcus

> **···**
>
> Am 19.12.2013 02:57, schrieb Ammar Ali:
> 
> > Here’s one way:
> > 
> > > > a1 = %w{a b c}
> > 
> > =\> ["a", "b", "c"]
> > 
> > > > a2 = %w{1 2 3}
> > 
> > =\> ["1", "2", "3"]
> > 
> > > > a1.map {|c1| a2.map {|c2| c1 + c2} }.flatten
> > 
> > =\> ["a1", "a2", "a3", "b1", "b2", "b3", "c1", "c2", "c3”]
> 
> --  
> GitHub: [stomar (Marcus Stollsteimer) · GitHub](https://github.com/stomar/)  
> PGP: 0x6B3A101Amm

---

<div class="post-metadata">

### Author: ![Ammar\_Ali](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@Ammar\_Ali](https://rubytalk.org/u/Ammar_Ali)
#### Post date: [19 December 2013 07:53 UTC](https://rubytalk.org/t/2-arrays-into-1-array-with-all-possible-combinations/68895/7 "2013-12-19T07:53:06Z")

</div>

I actually like Abinoam’s suggestion better.

a1.product(a2).map(&:join)

Regards,  
Ammar

> **···**
>
> On Dec 19, 2013, at 4:11 AM, Mike Lerner \<lists@ruby-forum.com\> wrote:
> 
> > That's it! Thank you!

---

<div class="post-metadata">

### Author: ![John\_Moon](https://avatars.discourse-cdn.com/v4/letter/j/c6cbf5/32.png) [@John\_Moon](https://rubytalk.org/u/John_Moon)
#### Post date: [20 December 2013 16:16 UTC](https://rubytalk.org/t/2-arrays-into-1-array-with-all-possible-combinations/68895/8 "2013-12-20T16:16:55Z")

</div>

ar3 = [ar1, ar2]  
ar3.flatten.permutation.map(&:join)

> **···**
>
> On Fri, Dec 20, 2013 at 6:27 AM, Selvag Ruby \<lists@ruby-forum.com\> wrote:
> 
> > ar1=['a','b','c']  
> > ar2=[1,2,3]  
> > ar3=Array.new()
> > 
> > ar1.each do |a| ar2.each do |n| ar3.push(a+n.to\_s) end end  
> > p ar3
> > 
> > --  
> > Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).
> 
> --  
> John Moon

---

<div class="post-metadata">

### Author: ![Ammar\_Ali](https://avatars.discourse-cdn.com/v4/letter/a/73ab20/32.png) [@Ammar\_Ali](https://rubytalk.org/u/Ammar_Ali)
#### Post date: [19 December 2013 07:50 UTC](https://rubytalk.org/t/2-arrays-into-1-array-with-all-possible-combinations/68895/9 "2013-12-19T07:50:50Z")

</div>

I did not notice that. Thanks for pointing it out.

Regards,  
Ammar

> **···**
>
> On Dec 19, 2013, at 7:31 AM, sto.mar@web.de wrote:
> 
> > @Ammar: "\>\>" is rendered as quote by many email clients,  
> > so using it as prompt is not so good in a mailing list.

---

<div class="post-metadata">

### Author: ![abinoam](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/abinoam/32/1887_2.png) [@abinoam](https://rubytalk.org/u/abinoam)
#### Post date: [19 December 2013 14:27 UTC](https://rubytalk.org/t/2-arrays-into-1-array-with-all-possible-combinations/68895/10 "2013-12-19T14:27:52Z")

</div>

Thanks.

Yukihiro Matsumoto: Ruby inherited the Perl philosophy of having MORE  
THAN ONE WAY to do the same thing.  
([http://www.artima.com/intv/rubyP.html\](http://www.artima.com/intv/rubyP.html%5C))

That's why it's a cool language !!! 😉

> **···**
>
> On Thu, Dec 19, 2013 at 4:53 AM, Ammar Ali \<ammarabuali@gmail.com\> wrote:
> 
> > On Dec 19, 2013, at 4:11 AM, Mike Lerner \<lists@ruby-forum.com\> wrote:
> > 
> > > That's it! Thank you!
> > 
> > I actually like Abinoam’s suggestion better.
> > 
> > a1.product(a2).map(&:join)
> > 
> > Regards,  
> > Ammar
