f



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

-- 
Posted via http://www.ruby-forum.com/.

0
tthackery (1)
12/28/2010 10:58:35 PM
comp.lang.ruby 48886 articles. 0 followers. Post Follow

9 Replies
1040 Views

Similar Articles

[PageSpeed] 59

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

0
Nathan
12/28/2010 11:07:34 PM
[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
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<



On Tue, Dec 28, 2010 at 5:58 PM, Thomas T. <tthackery@gmail.com> 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/.
>
>

0
Ian
12/28/2010 11:07:35 PM
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

-- 
Posted via http://www.ruby-forum.com/.

0
Thomas
12/28/2010 11:19:44 PM
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?

-- 
Posted via http://www.ruby-forum.com/.

0
Thomas
12/28/2010 11:40:20 PM
[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
On Dec 28, 2010 6:41 PM, "Thomas T." <tthackery@gmail.com> wrote:
> 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?
>
> --
> Posted via http://www.ruby-forum.com/.
>

0
Ian
12/28/2010 11:43:58 PM
[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.

On Tue, Dec 28, 2010 at 6:43 PM, Ian M. Asaff <ian.asaff@gmail.com> wrote:

> Yep.
>
> --sent from myu droid. typoos courtesy of droid's crappy keyboarsd
> On Dec 28, 2010 6:41 PM, "Thomas T." <tthackery@gmail.com> wrote:
> > 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?
> >
> > --
> > Posted via http://www.ruby-forum.com/.
> >
>

0
Adam
12/29/2010 3:13:56 AM
How about this?

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

Masa

2010/12/29 Adam Prescott <mentionuse@gmail.com>:
> 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.
>
> On Tue, Dec 28, 2010 at 6:43 PM, Ian M. Asaff <ian.asaff@gmail.com> wrote:
>
>> Yep.
>>
>> --sent from myu droid. typoos courtesy of droid's crappy keyboarsd
>> On Dec 28, 2010 6:41 PM, "Thomas T." <tthackery@gmail.com> wrote:
>> > 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?
>> >
>> > --
>> > Posted via http://www.ruby-forum.com/.
>> >
>>
>

0
Masaomi
12/29/2010 7:54:31 AM
[Note:  parts of this message were removed to make it a legal post.]

On Tue, Dec 28, 2010 at 5:19 PM, Thomas T. <tthackery@gmail.com> wrote:

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

0
Josh
12/29/2010 10:23:11 AM
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.

-- 
Posted via http://www.ruby-forum.com/.

0
Thomas
12/29/2010 8:08:08 PM
Reply: