From: "Ian M. Asaff" Date: 2010-12-29T08:07:35+09:00 Subject: Re: convert String "1;2;3;4;5;" to Array [1, 2, 3, 4, 5] --0016e6dd89077d2c66049880848d Content-Type: text/plain; charset=ISO-8859-1 you mean something like this? "1;2;3;4;5;6;7;8;9;10".split(';').inject([]) { |a,i| a << i.to_i } => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] looking below, where do 11, 12 come from in the desired result? >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> raw_data = "1;2;3;4;5;6;7;8;9;10" data = raw_data.split(/\;/) #but this gives ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"], not [1, 2, 3,...] #data = [1,2,3,4,5,6,7,8,9,10,11,12] # this is the desired result <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< On Tue, Dec 28, 2010 at 5:58 PM, Thomas T. wrote: > I'm trying to convert a String of numbers that are separated by > semicolons to an Array---totally for fun, to stretch my ruby > understanding, fyi. > > I use the Array in a while loop which does work when the Array looks > like = [1,2,3,4,5,...]---so that part is working. But I want to use ruby > to > convert a String = "1;2;3;4;5;6;7;8;9;10" into an Array [1,2,3,4,5,...] > so > that I can use these values. > > I've tried many a method, but can't seem to get the desired result; I've > tried gsub(/\;/, ","), eval (), and others. > > ########## > > raw_data = "1;2;3;4;5;6;7;8;9;10" > data = raw_data.split(/\;/) #but this gives ["1", "2", "3", "4", "5", > "6", "7", "8", "9", "10"], not [1, 2, 3,...] > > #data = [1,2,3,4,5,6,7,8,9,10,11,12] # this is the desired result > > boundary = 1 > ending_boundary = 13 > interval = (ending_boundary - boundary)/3 > > while boundary < ending_boundary > print "For the class #{boundary} to #{boundary + interval}, " > print "the group is: " > puts data.select{ |x| x >= boundary && x < (boundary + interval) > }.size > print data.select{ |x| x >= boundary && x < (boundary + interval) > }.join(' ') > boundary = boundary + interval #increase the boundary to the next > class > print ".\n" > end > > -- > Posted via http://www.ruby-forum.com/. > > --0016e6dd89077d2c66049880848d--