split string

S

Sijo Kg

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
 
D

Dan

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

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"]
 
A

Arlen Cuss

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

Hi,

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
 
S

Sijo Kg

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
 
A

Arlen Cuss

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

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
 
S

Sijo Kg

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
 
A

Arlen Cuss

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

Hi there,

For Rails, try Agile Web Development 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. :)

Arlen
 
7

7stud --

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

Sijo

Consider the new book:

The Ruby Programming Language

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

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

Similar Threads


Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top