[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

split string

Sijo Kg

2/20/2008 6:52:00 AM

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
--
Posted via http://www.ruby-....

8 Answers

dan.macdaddy+ruby

2/20/2008 7:00:00 AM

0

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


On Feb 20, 2008 5:22 PM, Sijo Kg <sijo@maxxion.com> wrote:

> Hi
>
> I have @selected_ciid_and_asso_types ( its content is
> , )
> 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
> --
> Posted via http://www.ruby-....
>
>

Arlen Cuss

2/20/2008 7:11:00 AM

0

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

Hi,

On Feb 20, 2008 6:00 PM, Dan <dan.macdaddy+ruby@gmail.com> 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

Sijo Kg

2/20/2008 7:40:00 AM

0

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
--
Posted via http://www.ruby-....

Arlen Cuss

2/20/2008 7:46:00 AM

0

[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

Sijo Kg

2/20/2008 8:01:00 AM

0

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

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

Arlen Cuss

2/20/2008 8:06:00 AM

0

[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

Sijo Kg

2/20/2008 8:09:00 AM

0

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

Sijo
--
Posted via http://www.ruby-....

7stud --

2/20/2008 8:45:00 AM

0

Sijo Kg wrote:
> 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.



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