string into array

J

Josselin

I have the following string
id = "(48.88689971942316, 2.3380279541015625)"
is there any method to transform directly into an array
[48.88689971942316, 2.3380279541015625] or should extract both parts
and create the array (what I did.. but not very DRY)

tfyl

joss
 
R

Robert Klemme

I have the following string
id = "(48.88689971942316, 2.3380279541015625)"
is there any method to transform directly into an array
[48.88689971942316, 2.3380279541015625] or should extract both parts and
create the array (what I did.. but not very DRY)

In this simple case you can do:
id = "(48.88689971942316, 2.3380279541015625)" => "(48.88689971942316, 2.3380279541015625)"
id.scan /\d+\.\d+/ => ["48.88689971942316", "2.3380279541015625"]
id.scan(/\d+\.\d+/).map! {|x| x.to_f}
=> [48.8868997194232, 2.33802795410156]

It depends on the values that can occur in the string if this is
sufficient (for example, the piece above does not take care of signs
also does not recognize numbers without decimals). You can find plenty
solutions for matching floating point numbers in the archives of this
list / newsgroup.

Generally, if you get this string from some piece of external code, you
want to parse it in some way to make sure it matches your expectations.

Kind regards

robert
 
J

Julian Tarkhanov

I have the following string
id = "(48.88689971942316, 2.3380279541015625)"
is there any method to transform directly into an array
[48.88689971942316, 2.3380279541015625] or should extract both
parts and create the array (what I did.. but not very DRY)

tfyl

id[1..-2].split(',').map {|e| e.to_f }
 
H

Hemant Kumar

I have the following string
id = "(48.88689971942316, 2.3380279541015625)"
is there any method to transform directly into an array
[48.88689971942316, 2.3380279541015625] or should extract both parts
and create the array (what I did.. but not very DRY)

tfyl

joss

Since, I dunno abt really DRY way of doing that, I would go with this:


class String
def to_farray
self.split(',').map {|part| part.gsub(/[()]/,"").to_f}
end
end
a = "(48.88689971942316, 2.3380279541015625)"
a.to_farray #=> [48.88689971942316, 2.3380279541015625]
 
J

Josselin

I have the following string
id = "(48.88689971942316, 2.3380279541015625)"
is there any method to transform directly into an array
[48.88689971942316, 2.3380279541015625] or should extract both parts
and create the array (what I did.. but not very DRY)

In this simple case you can do:
id = "(48.88689971942316, 2.3380279541015625)" => "(48.88689971942316, 2.3380279541015625)"
id.scan /\d+\.\d+/ => ["48.88689971942316", "2.3380279541015625"]
id.scan(/\d+\.\d+/).map! {|x| x.to_f}
=> [48.8868997194232, 2.33802795410156]

It depends on the values that can occur in the string if this is
sufficient (for example, the piece above does not take care of signs
also does not recognize numbers without decimals). You can find plenty
solutions for matching floating point numbers in the archives of this
list / newsgroup.

Generally, if you get this string from some piece of external code, you
want to parse it in some way to make sure it matches your expectations.

Kind regards

robert

thanks Robert, better that what I originally wrote : (suppressing
parenthesis then split on comma)

@point = id.tr('()', ' ').split(',')
 

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

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,143
Latest member
SterlingLa
Top