# Array Combinations

**URL:** https://rubytalk.org/t/array-combinations/47555
**Category:** ruby-talk
**Created:** [7 July 2008 04:13 UTC](https://rubytalk.org/t/array-combinations/47555 "2008-07-07T04:13:30Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Pragash\_Mr](https://avatars.discourse-cdn.com/v4/letter/p/71c47a/32.png) [@Pragash\_Mr](https://rubytalk.org/u/Pragash_Mr)
#### Post date: [7 July 2008 04:13 UTC](https://rubytalk.org/t/array-combinations/47555/1 "2008-07-07T04:13:30Z")

</div>

Hi,  
I want to find array combinations is there any way find combinations in  
array

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

---

<div class="post-metadata">

### Author: ![Mikel\_Lindsaar1](https://avatars.discourse-cdn.com/v4/letter/m/4491bb/32.png) [@Mikel\_Lindsaar1](https://rubytalk.org/u/Mikel_Lindsaar1)
#### Post date: [7 July 2008 04:32 UTC](https://rubytalk.org/t/array-combinations/47555/2 "2008-07-07T04:32:27Z")

</div>

Yes.

If you want a more detailed answer, it might be a good idea to give an  
appropriately detailed question... 🙂

Best Regards

Mikel

> **···**
>
> On Mon, Jul 7, 2008 at 2:13 PM, Pragash Mr. \<gananapragasam@srishtisoft.com\> wrote:
> 
> > I want to find array combinations is there any way find combinations in  
> > array
> 
> --
> 
> > **[Mikel Lindsaar](https://lindsaar.net/)**
> >
> > Internet Home for Mikel Lindsaar
> 
> Rails, RSpec and Life blog....

---

<div class="post-metadata">

### Author: ![Pragash\_Mr](https://avatars.discourse-cdn.com/v4/letter/p/71c47a/32.png) [@Pragash\_Mr](https://rubytalk.org/u/Pragash_Mr)
#### Post date: [7 July 2008 04:35 UTC](https://rubytalk.org/t/array-combinations/47555/3 "2008-07-07T04:35:02Z")

</div>

Mikel Lindsaar wrote:

> **···**
>
> > On Mon, Jul 7, 2008 at 2:13 PM, Pragash Mr. \> \<gananapragasam@srishtisoft.com\> wrote:
> > 
> > > I want to find array combinations is there any way find combinations in  
> > > array
> > 
> > Yes.
> > 
> > If you want a more detailed answer, it might be a good idea to give an  
> > appropriately detailed question... 🙂
> > 
> > Best Regards
> > 
> > Mikel
> 
> Hi,  
> i am having an array for ex:[1,2,3,4]
> 
> from this array i want to find the possible combinations like  
> [1,3,4,2]  
> [1,4,2,3]  
> etc....
> 
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

### Author: ![Martin\_DeMello](https://avatars.discourse-cdn.com/v4/letter/m/f17d59/32.png) [@Martin\_DeMello](https://rubytalk.org/u/Martin_DeMello)
#### Post date: [7 July 2008 04:40 UTC](https://rubytalk.org/t/array-combinations/47555/4 "2008-07-07T04:40:09Z")

</div>

[http://permutation.rubyforge.org/](http://permutation.rubyforge.org/)

martin

> **···**
>
> On Sun, Jul 6, 2008 at 9:35 PM, Pragash Mr. \<gananapragasam@srishtisoft.com\> wrote:
> 
> > i am having an array for ex:[1,2,3,4]
> > 
> > from this array i want to find the possible combinations like  
> > [1,3,4,2]  
> > [1,4,2,3]  
> > etc....

---

<div class="post-metadata">

### Author: ![\_Pena\_Botp1](https://avatars.discourse-cdn.com/v4/letter/_/94ad74/32.png) [@\_Pena\_Botp1](https://rubytalk.org/u/_Pena_Botp1)
#### Post date: [7 July 2008 05:13 UTC](https://rubytalk.org/t/array-combinations/47555/5 "2008-07-07T05:13:12Z")

</div>

# i am having an array for ex:[1,2,3,4]  
# from this array i want to find the possible combinations like  
# [1,3,4,2]  
# [1,4,2,3]  
# etc....

if you have ruby version 1.8.7 or 1.9, just try it

eg,

> RUBY\_VERSION

=\> "1.8.7"

> [1,2,3].permutation(3){|x| p x}

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

> [1,2,3].permutation(2){|x| p x}

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

> [1,2,3].combination(3){|x| p x}

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

> [1,2,3].combination(2){|x| p x}

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

kind regards -botp

> **···**
>
> From: Pragash Mr. [mailto:gananapragasam@srishtisoft.com]
