# Iterate through Array?

**URL:** <https://www.ruby-forum.com/t/iterate-through-array/121022>\
**Category:** Ruby\
**Created:** [November 22, 2007, 9:00pm UTC](https://www.ruby-forum.com/t/iterate-through-array/121022 "2007-11-22T21:00:55Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zephyr\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/aeb1de/32.png) [@Zephyr\_P](https://www.ruby-forum.com/u/Zephyr_P)\
**Post date:** [November 22, 2007, 9:00pm UTC](https://www.ruby-forum.com/t/iterate-through-array/121022/1 "2007-11-22T21:00:55Z")

</div>

I’ve been messing around with generating all possible combinations of a  
set of letters, (Ala, a 6 letter set would start enumerating at “aaaaaa”  
then move on to “aaaaab” so on and so forth, all the way down to  
“zzzzz”) by iterating through an array using slice() , Which is a pretty  
good implementation for small tasks, But suppose I wanted to generate  
larger sets, Is there a more efficient implementation than say

```
 a=0
 b=0
 c=0
 d=0
 e=0
 f=0
 z = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",

```

“m”, “n”, “o”, “p”, “q”, “r”, “s”, “t”, “u”, “v”, “w”, “x”, “y”, “z”]

slice = z.slice(a) + z.slice(b) + z.slice© + z.slice(d) + z.slice(e) +  
z.slice(f)

if…  
else… [Incrementing]

```
 puts slice

```

Thank you in advance

---

<div class="post-metadata">

**Author:** ![Zephyr\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/aeb1de/32.png) [@Zephyr\_P](https://www.ruby-forum.com/u/Zephyr_P)\
**Post date:** [November 22, 2007, 9:23pm UTC](https://www.ruby-forum.com/t/iterate-through-array/121022/2 "2007-11-22T21:23:08Z")

</div>

Zephyr P. wrote:

> I’ve been messing around with generating all possible combinations of a  
> set of letters, (Ala, a 6 letter set would start enumerating at “aaaaaa”  
> then move on to “aaaaab” so on and so forth, all the way down to  
> “zzzzz”)

(“aaaaaa”…“zzzzzz”).each do |string|  
puts string  
end

This will run for a good while, but it won’t hog memory the way a  
solution  
populating an array would.

HTH,  
Sebastian

---

<div class="post-metadata">

**Author:** ![Zephyr\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/aeb1de/32.png) [@Zephyr\_P](https://www.ruby-forum.com/u/Zephyr_P)\
**Post date:** [November 22, 2007, 9:26pm UTC](https://www.ruby-forum.com/t/iterate-through-array/121022/3 "2007-11-22T21:26:39Z")

</div>

You might also try the Permutations gem at  
[http://permutation.rubyforge.org/](http://permutation.rubyforge.org/)

---

<div class="post-metadata">

**Author:** ![Zephyr\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/aeb1de/32.png) [@Zephyr\_P](https://www.ruby-forum.com/u/Zephyr_P)\
**Post date:** [November 22, 2007, 9:33pm UTC](https://www.ruby-forum.com/t/iterate-through-array/121022/4 "2007-11-22T21:33:05Z")

</div>

> You might also try the Permutations gem …

Or use the Combinatorial Object Server and the Hpricot gem. Pass in your  
parameters for the combinatorial object and scrape your results out of  
the table cells. I don’t believe there is an API for the COS but  
scraping the hrml results is quite easy.

Combinatorial Object Server  
[http://theory.cs.uvic.ca/root.html](http://theory.cs.uvic.ca/root.html)

---

<div class="post-metadata">

**Author:** ![Zephyr\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/aeb1de/32.png) [@Zephyr\_P](https://www.ruby-forum.com/u/Zephyr_P)\
**Post date:** [November 23, 2007, 4:59am UTC](https://www.ruby-forum.com/t/iterate-through-array/121022/5 "2007-11-23T04:59:03Z")

</div>

What about something as simple as

i = 0  
“aaaaa”.upto(“zzzzz”) {|s| i += 1; puts “#{i}. #{s}”}

You could write to a file if you wish. Note that “aaaaa”.upto(“zzzzz”)  
is NOT the same as “a”.upto(“zzzzz”)

---

<div class="post-metadata">

**Author:** ![Zephyr\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/z/aeb1de/32.png) [@Zephyr\_P](https://www.ruby-forum.com/u/Zephyr_P)\
**Post date:** [November 22, 2007, 11:06pm UTC](https://www.ruby-forum.com/t/iterate-through-array/121022/6 "2007-11-22T23:06:50Z")

</div>

Sebastian H. wrote:

> This will run for a good while, but it won’t hog memory the way a solution  
> populating an array would.
> 
> HTH,  
> Sebastian

Impressive, Signifigantly less RAM usage.
