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

T

Thomas T.

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
 
N

Nathan Clark

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

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

Ian M. Asaff

[Note: parts of this message were removed to make it a legal post.]

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



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
 
T

Thomas T.

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
 
T

Thomas T.

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

Ian M. Asaff

[Note: parts of this message were removed to make it a legal post.]

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

Adam Prescott

[Note: parts of this message were removed to make it a legal post.]

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.

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

Masaomi Hatakeyama

How about this?

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

Masa

2010/12/29 Adam Prescott said:
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.

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

Josh Cheek

[Note: parts of this message were removed to make it a legal post.]

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 }
Go with map, this is its raison d'etre.
 
T

Thomas T.

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top