From: Daniel Date: 2007-03-24T08:03:42+09:00 Subject: Re: How to Convert String to Array On Sat, Mar 24, 2007 at 07:51:27AM +0900, Daniel wrote: > On Sat, Mar 24, 2007 at 07:36:31AM +0900, Shengzhi Li wrote: > > Hey everyone, > > > > Newb question: I want to read the user input an array of numbers. Is > > this possible? If not, then I want to convert the string input (from > > gets) into an array. > > > > For example: > > > > user inputs [5,3,46,6,5] > > gets.chomp converts this to a string "[5,3,46,6,5]" > > I want [5,3,46,6,5] (the arrray, not the string) > > a = "[5,3,46,6,5]" > a.gsub!(/[\[\]]/,'').split(/\s*,\s*/) > => ["5", "3", "46", "6", "5"] > > This removes the [] first, then splits on , missed the collect as mentioned in the other response. > > -d -- "You will never know how much it has cost my generation to preserve your freedom. I hope you will make good use of it." -- John Quincy Adams "Yes, we did produce a near-perfect republic. But will they keep it? Or will they, in the enjoyment of plenty, lose the memory of freedom? Material abundance without character is the path of destruction." -- Thomas Jefferson