Convert String "1;2;3;4;5;" to Array [1, 2, 3, 4, 5]

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

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,…]

This is the right track, you just need to then convert each string to
a number, e.g.

raw_data.split(";").map { |raw| raw.to_i }

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
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Thanks to both. I’m reading up on these methods you’ve supplied at
http://www.ruby-doc.org/core/. It looks like the .map method is a
synonym for .collect. I’ll read up on .inject next.

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]

Yes. In fact, both of these work—liking ruby’s flexibility here.

#data = raw_data.split(/;/).map { |raw| raw.to_f}
#data = raw_data.split(/;/).inject([]) { |a,i| a << i.to_f }

looking below, where do 11, 12 come from in the desired result? my
inconsistency.

Thomas

Yep.

–sent from myu droid. typoos courtesy of droid’s crappy keyboarsd

Ian M. Asaff wrote in post #971169:

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]

Ok, I’ve read the page on .inject, but do have a question on your
solution. I’m interested in the [] where I read an initial value may be
supplied in .inject([]). Is the [] initializing an empty array to which
the accumulator, a, will populate?

I would avoid the inject solution because it’s cumbersome and isn’t as
easy
to understand; map is pretty much to-the-point and doesn’t have this
weird
empty-array accumulator. The inject solution is just reinventing the map
wheel.

How about this?

eval("[" + “1;2;3”.gsub(/;/,’,’) + “]”)
=> [1,2,3]

Masa

2010/12/29 Adam P. [email protected]:

On Tue, Dec 28, 2010 at 5:19 PM, Thomas T. [email protected] wrote:

Yes. In fact, both of these work—liking ruby’s flexibility here.

#data = raw_data.split(/;/).map { |raw| raw.to_f}
#data = raw_data.split(/;/).inject([]) { |a,i| a << i.to_f }

Go with map, this is its raison d’etre.

Thank you all for your assistance. Calling the .methods on a string like
I had was rather overwhelming, and many of the RDoc definitions are
still opaque to me.