From: 7stud -- Date: 2009-02-07T02:20:33+09:00 Subject: Re: Dynamically create an unknown number of arrays Shandy Nantz 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] -- Posted via http://www.ruby-forum.com/.