Split string

Hi

I have @selected_ciid_and_asso_types ( its content is
3:43,2:65,3:50, )
How can i split it like

3 43
2 65
3 50

These are actually two columns of a table…If i get it like above I can
directly save it to database

Sijo

I’m not sure if i really understand your question but if 3:43,2:65,3:50
is
actually a string i.e. “3:43,2:65,3:50”

then “3:43,2:65,3:50”.gsub(’:’,’ ‘).split(’,’) will return an array
containing:

[“3 43”, “2 65”, “3 50”]

Hi,

On Feb 20, 2008 6:00 PM, Dan [email protected] wrote:

I’m not sure if i really understand your question but if 3:43,2:65,3:50 is
actually a string i.e. “3:43,2:65,3:50”

then “3:43,2:65,3:50”.gsub(‘:’,’ ‘).split(’,') will return an array
containing:

[“3 43”, “2 65”, “3 50”]

If they’re two columns of a database, then this will probably be more
helpful:

rows = @selected_ciid_and_asso_types.split(‘,’).map {|e| e.split(‘:’)}
=> [[“3”, “43”], [“2”, “65”], [“3”, “50”]]

You could also change the last bit to e.split(‘:’).map {|s| s.to_i} if
you
wanted [[3, 43], [2, 65], [3, 50]] instead.

Arlen

Hi
This is working…But what I need is like
ci_id service_desk_ci_id

3 43
2 65
3 50

Then with in a for loop

@obj.ci_id=3
@obj.service_desk_ci_id=43

@obj.ci_id=2
@obj.service_desk_ci_id=65

@obj.ci_id=3
@obj.service_desk_ci_id=50

How can i do this ?

Sijo

Hi
Thanks a lot…It is working…To get more understanding of all these
can u suggest me a good book or online tutorial…I am a beginner to
rails

Sijo

Hi,

rows = @selected_ciid_and_asso_types.split(’,’).map {|e|
e.split(’:’).map
{|s| s.to_i}}
rows.each do |r|
obj = make_obj_somehow()
obj.ci_id, obj.service_desk_ci_id = r
end

Something like this?

Arlen

Hi there,

For Rails, try Agile Web D. with Rails: A Pragmatic Guide - it
got
me off the ground in Rails, but you’ll still need to put in effort to
grasp
the situation clearly. Learning the ins and outs of Ruby is important.
:slight_smile:

Arlen

Thanks again .For ruby learning may you suggest a good book

Sijo

Sijo Kg wrote:

Thanks again .For ruby learning may you suggest a good book

Sijo

Consider the new book:

The Ruby P.ming Language

One of the authors, David Flanagan, previously wrote ‘the’ book for
Javascript programming, so I would expect this book to be top notch.