# Create a variable number of loop

**URL:** <https://www.ruby-forum.com/t/create-a-variable-number-of-loop/172418>\
**Category:** Ruby\
**Created:** [July 21, 2009, 10:39am UTC](https://www.ruby-forum.com/t/create-a-variable-number-of-loop/172418 "2009-07-21T10:39:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [July 21, 2009, 10:39am UTC](https://www.ruby-forum.com/t/create-a-variable-number-of-loop/172418/1 "2009-07-21T10:39:55Z")

</div>

Hi all,

I want to create a variable number of loop based on the input:  
if the input is 1, I will create a loop ( I call it 1-loop);  
if the input is 2 , I will create a nested loop( 2-loop);  
if the input is 3, I will create a loop of loop of loop (3-loop);  
if the input is 4, I will create a 4-loop, so on and so on.  
How I can do that?

Thanks,

Li

array=%W{A B C D}

#input is 1

array.each do |e1|  
puts e1  
end

#input is 2  
array.each do |e1|  
array.each do |e|2  
puts e1+e2  
end  
end

#input is 4  
array.each do |e1|  
array.each do |e2|  
array.each do |e3|  
array.each do |e4|  
puts e1+e2+e3+e4  
end  
end  
end  
end

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [July 21, 2009, 10:49am UTC](https://www.ruby-forum.com/t/create-a-variable-number-of-loop/172418/2 "2009-07-21T10:49:48Z")

</div>

Would recursive methods be allowed in Ruby?

regards,  
Vikas Sarin

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [July 21, 2009, 10:59am UTC](https://www.ruby-forum.com/t/create-a-variable-number-of-loop/172418/3 "2009-07-21T10:59:10Z")

</div>

2009/7/21 Li Chen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> Hi all,
> 
> I want to create a variable number of loop based on the input:  
> if the input is 1, I will create a loop ( I call it 1-loop);  
> if the input is 2 , I will create a nested loop( 2-loop);  
> if the input is 3, I will create a loop of loop of loop (3-loop);  
> if the input is 4, I will create a 4-loop, so on and so on.  
> How I can do that?

Would this input be the size of the array you’re looping over? Seems  
from  
your example that you’re trying to sum all the combinations of values in  
the  
array. If so there’s probably a clean recursive way to do this.

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [July 21, 2009, 11:32am UTC](https://www.ruby-forum.com/t/create-a-variable-number-of-loop/172418/4 "2009-07-21T11:32:30Z")

</div>

2009/7/21 Li Chen [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> ```
> array.each do |e3|
> array.each do |e4|
> puts e1+e2+e3+e4
> end
> end
> 
> ```
> 
> end  
> end

You can do this recursively by decrementing a depth count until you hit  
the  
bottom, at which point to print the data you want:

def print\_sums(array, depth, memo = 0)  
array.each do |x|  
if depth == 1  
puts memo + x  
else  
print\_sums(array, depth - 1, memo + x)  
end  
end  
end

data = %W{A B C D}  
print\_sums(data, 1)  
print\_sums(data, 2)  
print\_sums(data, 3)  
print\_sums(data, 4)

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [July 21, 2009, 5:49pm UTC](https://www.ruby-forum.com/t/create-a-variable-number-of-loop/172418/5 "2009-07-21T17:49:18Z")

</div>

> You can do this recursively by decrementing a depth count until you hit  
> the  
> bottom, at which point to print the data you want:

Hi James,

Thank you very much for the codes. It solves part of my problem. I try  
to follow your idea and  
implement the reamining part. But I don’t think I can. Here is the  
whole story:

What I really try to do is to create a word of using letters of  
‘A,B,C,D’’ only with different size  
(1-letter, 2-letter,3-letter, …, up to 25-letter word).  
For each word created I count the value for each individual letter and  
get the sum for the word.

And here are the rules:  
A=0; B=1;C=2;D=3  
for a word of 1 letter its value is:

‘A’=\>0; ‘B’=\>1;’‘C’=\>2; '‘D’=\>3;

for ‘A’, it has 0\*(4**0) if it appears on 0th, 0\*(4**1) if on 10th,  
0\*(4**2) if on 100th, so on and so on  
for ‘B’, it has 1\*(4**0) if it appears on 0th, 1\*(4**1) if on 10th,  
1\*(4**2) if on 100th, so on and so on  
similar to ‘C’ and ‘D’

so the following word of ‘ABCD’ has a value: 0\*(4**3)+ 1\*(4**2)+  
2\*\*(4**1)+3\*(4**0)  
‘DDDD’ has a value:3\*(4**3)+ 3\*(4**2)+ 3\*\*(4**1)+3\*(4**0)

Li

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [July 22, 2009, 5:28pm UTC](https://www.ruby-forum.com/t/create-a-variable-number-of-loop/172418/6 "2009-07-22T17:28:46Z")

</div>

steve wrote:

> Li Chen wrote:
> 
> > ‘A’=\>0; ‘B’=\>1;’‘C’=\>2; '‘D’=\>3;
> > 
> > Li
> 
> C:\Documents and Settings\Administrator\>irb  
> irb(main):001:0\> “DDDD”.tr(“DCBA”,“3210”).to\_i(4)  
> =\> 255  
> irb(main):002:0\>

Steve,

Thank you so much. It is so neat!

Li

---

<div class="post-metadata">

**Author:** ![alex-osu3](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/13edae/32.png) [@alex-osu3](https://www.ruby-forum.com/u/alex-osu3)\
**Post date:** [July 22, 2009, 11:45am UTC](https://www.ruby-forum.com/t/create-a-variable-number-of-loop/172418/7 "2009-07-22T11:45:53Z")

</div>

Li Chen wrote:

> ‘A’=\>0; ‘B’=\>1;’‘C’=\>2; '‘D’=\>3;
> 
> Li

C:\Documents and Settings\Administrator\>irb  
irb(main):001:0\> “DDDD”.tr(“DCBA”,“3210”).to\_i(4)  
=\> 255  
irb(main):002:0\>
