Split the string

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

On 3/3/08, Sijo Kg [email protected] wrote:

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

Thanks for your reply

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

On 3/3/08, Sijo Kg [email protected] 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

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

On Behalf Of Sijo Kg:

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”]]

#…

Here i have to get the first 2 fields as integer and the third as

string

read on split, arrays, and enumerators.

this is just a simple example,

irb(main):037:0> require ‘enumerator’
=> false

irb(main):038:0> s=“3:43;SD,2:65;SD,3:50;Inc”
=> “3:43;SD,2:65;SD,3:50;Inc”

irb(main):039:0> a=[]
=> []

irb(main):040:0> s.split(/,|:|;/).each_slice(3){|x| a <<
[x[0].to_i,x[1].to_i,x[2]]}
=> nil

irb(main):041:0> a
=> [[3, 43, “SD”], [2, 65, “SD”], [3, 50, “Inc”]]

kind regards -botp

Thanks a lot for the replies

Sijo

On Mon, Mar 3, 2008 at 2:31 PM, Sijo Kg [email protected] 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

On 3/3/08, Michael F. [email protected] wrote:

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

On 3/3/08, Sijo Kg [email protected] 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

Sijo Kg wrote:

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”]]

Michael F. wrote:

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”]]

but he wants all strings in his array

From: S2 [mailto:[email protected]]

Michael F. wrote:

> 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”]]

but he wants all strings in his array

no, if you scan the thread, the op later wanted the first 2 elements be
integers.

kind regards -botp

hi
its a simple question yet google wasnt of much help

string = a_b_c_d_e

search results mostly show how to split it as a b c d e

but how to split it as a_b and c_d_e ?

On Jul 18, 2011, at 00:13 , suprabha r. wrote:

hi
its a simple question yet google wasnt of much help

string = a_b_c_d_e

search results mostly show how to split it as a b c d e

but how to split it as a_b and c_d_e ?

Your question is too arbitrary. What criteria are you hoping to split it
by? Index? Content? Randomly? I can come up with many answers to your
question, but that doesn’t mean it is the right answer.

Have you looked directly at the documentation for String? Try typing ri String and then maybe ri String#index into your terminal.

From: Christopher S. [mailto:[email protected]]

On 3/3/08, Michael F. [email protected] wrote:

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

careful, you might want to benchmark that, too.

a sample run on a windows machine,

C:\Documents and Settings\botp>cat test.rb

require ‘benchmark’
require ‘enumerator’
require ‘scanf’

n=1_000
s=“3:43;SD,2:65;SD,3:50;Inc”

puts RUBY_VERSION,RUBY_PLATFORM

Benchmark.bmbm do |x|
x.report(“scan”) do
n.times do
s.scan(/(\d+):(\d+);(\w+)/).map do |x| [x[0].to_i,x[1].to_i,x[2]]
e
end
end
x.report(“slice”) do
n.times do
a=[]
s.split(/,|:|;/).each_slice(3){|x| a <<
[x[0].to_i,x[1].to_i,x[2]]}
end
end
x.report(“split2”) do
n.times do
s.split(/,/).map do |i|
t = i.split(/:|;/)
t[0…1].map { |j| j.to_i } << t[2]
end
end
end
x.report(“scanf”) do
n.times do
s.split(‘,’).map do |e| e.scanf(‘%d:%d;%s’) end
end
end

end

C:\Documents and Settings\peñaijm>ruby test.rb
1.8.6
i386-mswin32
Rehearsal ------------------------------------------
scan 0.063000 0.000000 0.063000 ( 0.063000)
slice 0.078000 0.000000 0.078000 ( 0.079000)
split2 0.109000 0.000000 0.109000 ( 0.110000)
scanf 2.938000 0.015000 2.953000 ( 3.188000)
--------------------------------- total: 3.203000sec

         user     system      total        real

scan 0.063000 0.000000 0.063000 ( 0.063000)
slice 0.093000 0.000000 0.093000 ( 0.094000)
split2 0.125000 0.000000 0.125000 ( 0.125000)
scanf 2.984000 0.032000 3.016000 ( 3.031000)

kind regards -botp

suprabha r. wrote in post #1011335:

hi
its a simple question yet google wasnt of much help

string = a_b_c_d_e

search results mostly show how to split it as a b c d e

but how to split it as a_b and c_d_e ?

Well, one obvious way is to split it into [“a”,“b”,“c”,“d”,“e”] as
google showed, select the bits you want, then join them back together
again. Look at Array#join.

["a","b"].join("_")  # => "a_b"

Another way is to use a regular expression to match the patterns you’re
looking for. e.g.

if string =~ /\A([^_]*_[^_]*)_(.*)\z/
  str1, str2 = $1, $2
else
  raise "Unexpected format"
end

There are many resources on ruby regular expressions you can google for.