[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Split the string

Sijo Kg

3/3/2008 5:32:00 AM

I have a string
@selected_related_model_id_assotype_and_name=params[:relatedidmodelasso]

This is of the form
3:43;SD,2:65;SD,3:50;Inc

How can i split this to get

=>[["3","43","SD"],["2","65","SD"],["3","50","Inc"]]

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

13 Answers

Christopher Swasey

3/3/2008 5:42:00 AM

0

On 3/3/08, Sijo Kg <sijo@maxxion.com> wrote:
> I have a string
> @selected_related_model_id_assotype_and_name=params[:relatedidmodelasso]
>
> This is of the form
> 3:43;SD,2:65;SD,3:50;Inc
>
> How can i split this to get
>
> =>[["3","43","SD"],["2","65","SD"],["3","50","Inc"]]
>
> Sijo

First split by the comma, then #map the result and split using a conditional:

"3:43;SD,2:65;SD,3:50;Inc".split(/,/).map { |i| i.split(/:|;/) }
=> [["3", "43", "SD"], ["2", "65", "SD"], ["3", "50", "Inc"]]

Christopher

Sijo Kg

3/3/2008 5:52:00 AM

0

Thanks for your reply
--
Posted via http://www.ruby-....

Sijo Kg

3/3/2008 6:06:00 AM

0

Sir

Here i have to get the first 2 fields as integer and the third as
string.Could u please tell how can i do that in the map?

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

Christopher Swasey

3/3/2008 6:38:00 AM

0

On 3/3/08, Sijo Kg <sijo@maxxion.com> wrote:
> Sir
>
> Here i have to get the first 2 fields as integer and the third as
> string.Could u please tell how can i do that in the map?

Not so elegantly.

Map doesn't come in handy here because the string would get converted
to a 0. What you can do is hold the split inside a temporary variable
and construct another array, as such:

arr = "3:43;SD,2:65;SD,3:50;Inc".split(/,/).map do |i|
t = i.split(/:|;/)
t[0..1].map { |j| j.to_i } << t[2]

### If you're using Rails you can use Symbol#to_proc to simplify the code
# t[0..1].map(&:to_i) << t[2]

### or something very simple, if a tiny bit repetitive:
# [t[0].to_i, t[1].to_i, t[2]]
end
=> [[3, 43, "SD"], [2, 65, "SD"], [3, 50, "Inc"]]

Christopher

Sijo Kg

3/3/2008 6:49:00 AM

0

Here what is the meaning of this

t[0..1].map(&:to_i) << t[2]

Could u please explain details what split and map does?I am beginner in
ruby and ror

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

Peña, Botp

3/3/2008 7:20:00 AM

0

T24gQmVoYWxmIE9mIFNpam8gS2c6DQojIFRoaXMgaXMgb2YgdGhlIGZvcm0NCiMgMzo0MztTRCwy
OjY1O1NELDM6NTA7SW5jDQojIEhvdyBjYW4gaSBzcGxpdCB0aGlzIHRvIGdldA0KIyA9PltbIjMi
LCI0MyIsIlNEIl0sWyIyIiwiNjUiLCJTRCJdLFsiMyIsIjUwIiwiSW5jIl1dDQojLi4uDQojIEhl
cmUgaSBoYXZlIHRvIGdldCB0aGUgZmlyc3QgMiBmaWVsZHMgYXMgaW50ZWdlciBhbmQgdGhlIHRo
aXJkIGFzIA0KIyBzdHJpbmcNCg0KcmVhZCBvbiBzcGxpdCwgYXJyYXlzLCBhbmQgZW51bWVyYXRv
cnMuDQoNCnRoaXMgaXMganVzdCBhIHNpbXBsZSBleGFtcGxlLA0KDQppcmIobWFpbik6MDM3OjA+
IHJlcXVpcmUgJ2VudW1lcmF0b3InDQo9PiBmYWxzZQ0KDQppcmIobWFpbik6MDM4OjA+IHM9IjM6
NDM7U0QsMjo2NTtTRCwzOjUwO0luYyIgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ID0+ICIzOjQzO1NELDI6NjU7U0QsMzo1MDtJbmMiDQoNCmlyYihtYWluKTowMzk6MD4gYT1bXQ0K
PT4gW10NCg0KaXJiKG1haW4pOjA0MDowPiBzLnNwbGl0KC8sfDp8Oy8pLmVhY2hfc2xpY2UoMyl7
fHh8IGEgPDwgW3hbMF0udG9faSx4WzFdLnRvX2kseFsyXV19DQo9PiBuaWwNCg0KaXJiKG1haW4p
OjA0MTowPiBhDQo9PiBbWzMsIDQzLCAiU0QiXSwgWzIsIDY1LCAiU0QiXSwgWzMsIDUwLCAiSW5j
Il1dDQoNCg0Ka2luZCByZWdhcmRzIC1ib3RwDQo=

Christopher Swasey

3/3/2008 7:21:00 AM

0

On 3/3/08, Sijo Kg <sijo@maxxion.com> wrote:
> Here what is the meaning of this
>
>
> t[0..1].map(&:to_i) << t[2]
>
>
> Could u please explain details what split and map does?I am beginner in
> ruby and ror

The code, for reference:

arr = "3:43;SD,2:65;SD,3:50;Inc".split(/,/).map do |i|
t = i.split(/:|;/)
t[0..1].map(&:to_i) << t[2]
end

First, what we're doing here is splitting the string into an array by
the commas. This gets us
=> ["3:43;SD", "2:65;SD", "3:50;Inc"]

Then we're iterating through the resulting array using #map. #map is
provided by Enumerable. It iterates through all the elements of a
collection and combines the results into a new array.

#map passes each element to a block, one at a time. So, the first
element that gets passsed to the block is the string "3:43;SD". What's
important about the block is that the last expression to be evaluated
gets returned, and #map will add the returned value onto the array
that it's building. Inside the block we can do whatever we need to to
build the returned value.

In this case, we want the returned value to be a second array with the
individual parts of the current string split up. If it didn't matter
whether or not the resulting values were strings or integers then we
could just call #split on the string and leave it at that, as
"3:43;SD".split(/:\;/)
returns
=> ["3", "43", "SD"]
which, as the last expression evaluated, would get returned by the
block and pushed onto the array being built by #map.

You can think of #map as a function that transforms a collection, item
by item, and gives you back a new collection of the same size with the
same transformation applied to each constituent element. In this case,
we're taking an array of strings, transforming each individual string
into its own array, and we end up with an array of arrays.
=> [["3", "43", "SD"], ["2", "65", "SD"], ["3", "50", "Inc"]]

Now, in this case all we've done is split a bunch of strings, so each
ultimate element of the array is still going to be a string, even when
they appear to be numbers and we want them to be integers. Ideally,
what we'd do is a nested #map that called #to_i on each element to
convert them to integers:
i.split(/:|;/).map { |j| j.to_i }
The problem is that we want the third element to remain a string.
Calling #to_i on "SD" will return
=> 0

Which brings us to
t[0..1].map(&:to_i) << t[2]

What this does is slice out the first two elements of the array that
the #split gave us. This results in an array. In this case t[0..1]
would give us
=> ["3", "43"]

Then we're mapping that to a new array by calling to_i on each element
=> [3, 43]

Finally, we're pushing the string that we want to keep ("SD") onto the end
[3, 43] << t[2]
=> [3, 43, "SD"]

As the last expression to be evaluated, that completed array is
returned to #map and gets pushed onto the final, resulting array.

Now, I kind of skipped over a bit of code.
t[0..1].map(&:to_i) << t[2]
Specifically, the "&:to_i" part. This is somewhat advanced, and I'd
suggest that you read up on blocks and Procs ASAP. This is a good
opportunity to do so. Such knowledge is a prerequisite to
understanding Symbol#to_proc. (Symbol#to_proc, btw, isn't part of Ruby
core, it's a feature of Rails, although not exclusively).

To put it in simple terms, prepending & to a symbol results in
#to_proc being called on that symbol. #to_proc returns a simple block
which accepts an object and calls the specified method. In this case,
[].map(&:to_i)
is the equivalent of
[].map { |i| i.to_i }

Again, it's very simple and neat and efficient if you understand how
all the pieces of Ruby link together to make it possible. Once you
read up on Procs and blocks it should make sense.

Christopher

Michael Fellinger

3/3/2008 8:02:00 AM

0

On Mon, Mar 3, 2008 at 2:31 PM, Sijo Kg <sijo@maxxion.com> wrote:
> I have a string
> @selected_related_model_id_assotype_and_name=params[:relatedidmodelasso]
>
> This is of the form
> 3:43;SD,2:65;SD,3:50;Inc
>
> How can i split this to get
>
> =>[["3","43","SD"],["2","65","SD"],["3","50","Inc"]]

require 'scanf'
"3:43;SD,2:65;SD,3:50;Inc".split(',').map do |e|
e.scanf('%d:%d;%s')
end
# [[3, 43, "SD"], [2, 65, "SD"], [3, 50, "Inc"]]

^ manveru

Christopher Swasey

3/3/2008 8:16:00 AM

0

On 3/3/08, Michael Fellinger <m.fellinger@gmail.com> wrote:
> On Mon, Mar 3, 2008 at 2:31 PM, Sijo Kg <sijo@maxxion.com> wrote:
> > I have a string
> > @selected_related_model_id_assotype_and_name=params[:relatedidmodelasso]
> >
> > This is of the form
> > 3:43;SD,2:65;SD,3:50;Inc
> >
> > How can i split this to get
> >
> > =>[["3","43","SD"],["2","65","SD"],["3","50","Inc"]]
>
>
> require 'scanf'
> "3:43;SD,2:65;SD,3:50;Inc".split(',').map do |e|
> e.scanf('%d:%d;%s')
> end
> # [[3, 43, "SD"], [2, 65, "SD"], [3, 50, "Inc"]]
>
> ^ manveru

We have a winner!

Christopher

S2

3/3/2008 3:01:00 PM

0

Sijo Kg wrote:
> I have a string
> @selected_related_model_id_assotype_and_name=params[:relatedidmodelasso]
>
> This is of the form
> 3:43;SD,2:65;SD,3:50;Inc
>
> How can i split this to get
>
> =>[["3","43","SD"],["2","65","SD"],["3","50","Inc"]]
>
> Sijo


z = Array.new
"3:43;SD,2:65;SD,3:50;Inc".split(',').each {|n| z << n.split(/:|;/)}
z

=> [["3", "43", "SD"], ["2", "65", "SD"], ["3", "50", "Inc"]]