# Dynamically create an unknown number of arrays

**URL:** <https://www.ruby-forum.com/t/dynamically-create-an-unknown-number-of-arrays/159644>\
**Category:** Ruby\
**Created:** [February 6, 2009, 5:22pm UTC](https://www.ruby-forum.com/t/dynamically-create-an-unknown-number-of-arrays/159644 "2009-02-06T17:22:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [February 6, 2009, 5:22pm UTC](https://www.ruby-forum.com/t/dynamically-create-an-unknown-number-of-arrays/159644/1 "2009-02-06T17:22:36Z")

</div>

I need to create an unknow number of arrays on the fly (could be 1 array  
could be 10, I don’t know), is there a way to do this in Ruby and also  
name them dynamically? I also need to set those array’s to a collection  
of stuff found in a table. My Ruby is pretty rusty, and I am not sure if  
I can simply set an array to a group of stuff. If I have rows in a table  
that can be accessed by saying @user.table\_name, can I say:

a = Array.new  
a = @user.table\_name

Thanks for any help,

-S

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [February 6, 2009, 6:05pm UTC](https://www.ruby-forum.com/t/dynamically-create-an-unknown-number-of-arrays/159644/2 "2009-02-06T18:05:34Z")

</div>

On 06.02.2009 17:21, Shandy N. wrote:

> I need to create an unknow number of arrays on the fly (could be 1 array  
> could be 10, I don’t know), is there a way to do this in Ruby and also  
> name them dynamically? I also need to set those array’s to a collection  
> of stuff found in a table. My Ruby is pretty rusty, and I am not sure if  
> I can simply set an array to a group of stuff. If I have rows in a table  
> that can be accessed by saying @user.table\_name, can I say:
> 
> a = Array.new  
> a = @user.table\_name

You can say, or rather: write, that - but the effect would be  
disappointing. The second assignment simply overwrites the first one  
rendering it completely superfluous.

You can do this

arrays = Array.new(how\_many) {[]}

Cheers

robert

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [February 6, 2009, 6:21pm UTC](https://www.ruby-forum.com/t/dynamically-create-an-unknown-number-of-arrays/159644/3 "2009-02-06T18:21:15Z")

</div>

Shandy N. wrote:

> I need to create an unknow number of arrays on the fly (could be 1 array  
> could be 10, I don’t know), is there a way to do this in Ruby and also  
> name them dynamically? I also need to set those array’s to a collection  
> of stuff found in a table. My Ruby is pretty rusty, and I am not sure if  
> I can simply set an array to a group of stuff. If I have rows in a table  
> that can be accessed by saying @user.table\_name, can I say:
> 
> a = Array.new  
> a = @user.table\_name
> 
> Thanks for any help,
> 
> -S

## –data.txt– apple, banana, strawberry 10, 20, 30, 40 red blue green yellow black 100 200 300 a b c d

data = []  
num\_arrays = rand(5)

File.open(“data.txt”) do |f|  
num\_arrays.times do  
line = f.gets.strip()  
data \<\< line.split("/,?\s\*/")  
end  
end

p data

–output:–  
[[“apple, banana, strawberry”], [“10, 20, 30, 40”], [“red blue green  
yellow black”]]

The names of the arrays are: data[0], data[1], data[2]

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [February 6, 2009, 6:26pm UTC](https://www.ruby-forum.com/t/dynamically-create-an-unknown-number-of-arrays/159644/4 "2009-02-06T18:26:36Z")

</div>

> You can say, or rather: write, that - but the effect would be  
> disappointing. The second assignment simply overwrites the first one  
> rendering it completely superfluous.
> 
> You can do this
> 
> arrays = Array.new(how\_many) {[]}
> 
> Cheers
> 
> robert  
> I have done some digging around and this is what I have written down on  
> paper - not tested yet at all:

@array\_count = @user.tabledef.count #returns a 3, for example  
@count = 0

until @count == @array\_count - 1 do  
eval("#{@count} = @user.tabledefs["#{count}.items)  
@count += 1  
end

What I was hoping is that this would return 3 distintive arrays, is this  
not correct? What I am trying to do is build a table with x many columns  
and y many rows but before hand I know neither what x or y is going to  
be.

Also, assuming the above codes works (and assuming that there is not an  
easier way to do this, which I’m thinking there is) how do I access the  
arrays? Are they stored in a hash? or a variable? Thanks,

-S

---

<div class="post-metadata">

**Author:** ![snantz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/c2a13f/32.png) [@snantz](https://www.ruby-forum.com/u/snantz)\
**Post date:** [February 6, 2009, 6:34pm UTC](https://www.ruby-forum.com/t/dynamically-create-an-unknown-number-of-arrays/159644/5 "2009-02-06T18:34:24Z")

</div>

7stud – wrote:

> Shandy N. wrote:
> 
> > I need to create an unknow number of arrays on the fly (could be 1 array  
> > could be 10, I don’t know), is there a way to do this in Ruby and also  
> > name them dynamically? I also need to set those array’s to a collection  
> > of stuff found in a table. My Ruby is pretty rusty, and I am not sure if  
> > I can simply set an array to a group of stuff. If I have rows in a table  
> > that can be accessed by saying @user.table\_name, can I say:
> > 
> > a = Array.new  
> > a = @user.table\_name
> > 
> > Thanks for any help,
> > 
> > -S
> 
> ## –data.txt– apple, banana, strawberry 10, 20, 30, 40 red blue green yellow black 100 200 300 a b c d
> 
> data = []  
> num\_arrays = rand(5)
> 
> File.open(“data.txt”) do |f|  
> num\_arrays.times do  
> line = f.gets.strip()  
> data \<\< line.split("/,?\s\*/")  
> end  
> end
> 
> p data
> 
> –output:–  
> [[“apple, banana, strawberry”], [“10, 20, 30, 40”], [“red blue green  
> yellow black”]]
> 
> The names of the arrays are: data[0], data[1], data[2]

Whoops. No quotes around regex’s in ruby, and that regex doesn’t work  
anyway.

If you have comma delimited data like this:

## –data.txt----------- apple, banana, strawberry 10, 20, 30, 40 red, blue, green, yellow, black 100, 200, 300 a, b, c, d

you can use this for the regex:

pieces = line.split(/,\s\*/)

If your data is space delimited, you can use:

pieces = line.split(/\s+/)
