```--001636c5b35675bce3046f33ea41
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 7bit

2009/7/21 Li Chen <chen_li3 / yahoo.com>

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

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

--
James Coglan
http://jcoglan.com

--001636c5b35675bce3046f33ea41--

```