# String into array

**URL:** <https://rubytalk.org/t/string-into-array/34871>\
**Category:** ruby-talk\
**Created:** [28 January 2007 10:15 UTC](https://rubytalk.org/t/string-into-array/34871 "2007-01-28T10:15:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Josselin](https://avatars.discourse-cdn.com/v4/letter/j/9de0a6/32.png) [@Josselin](https://rubytalk.org/u/Josselin)\
**Post date:** [28 January 2007 10:15 UTC](https://rubytalk.org/t/string-into-array/34871/1 "2007-01-28T10:15:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Julian\_Julik\_Tarkhan](https://avatars.discourse-cdn.com/v4/letter/j/5e9695/32.png) [@Julian\_Julik\_Tarkhan](https://rubytalk.org/u/Julian_Julik_Tarkhan)\
**Post date:** [28 January 2007 10:24 UTC](https://rubytalk.org/t/string-into-array/34871/2 "2007-01-28T10:24:52Z")

</div>

id[1..-2].split(',').map {|e| e.to\_f }

> **···**
>
> On Jan 28, 2007, at 11:15 AM, Josselin wrote:
> 
> > 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
> 
> --  
> Julian 'Julik' Tarkhanov  
> please send all personal mail to  
> me at julik.nl

---

<div class="post-metadata">

**Author:** ![Robert\_K1](https://yyz1.discourse-cdn.com/flex029/user_avatar/rubytalk.org/robert_k1/32/1830_2.png) [@Robert\_K1](https://rubytalk.org/u/Robert_K1)\
**Post date:** [28 January 2007 10:25 UTC](https://rubytalk.org/t/string-into-array/34871/3 "2007-01-28T10:25:07Z")

</div>

> 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

&nbsp;&nbsp;robert

> **···**
>
> On 28.01.2007 11:13, Josselin wrote:

---

<div class="post-metadata">

**Author:** ![Hemant\_Kumar](https://avatars.discourse-cdn.com/v4/letter/h/46a35a/32.png) [@Hemant\_Kumar](https://rubytalk.org/u/Hemant_Kumar)\
**Post date:** [28 January 2007 10:32 UTC](https://rubytalk.org/t/string-into-array/34871/4 "2007-01-28T10:32:13Z")

</div>

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

class String  
&nbsp;&nbsp;def to\_farray  
&nbsp;&nbsp;&nbsp;&nbsp;self.split(',').map {|part| part.gsub(/[()]/,"").to\_f}  
&nbsp;&nbsp;end  
end  
a = "(48.88689971942316, 2.3380279541015625)"  
a.to\_farray #=\> [48.88689971942316, 2.3380279541015625]

> **···**
>
> On Sun, 2007-01-28 at 19:15 +0900, Josselin wrote:
> 
> > 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

---

<div class="post-metadata">

**Author:** ![Josselin](https://avatars.discourse-cdn.com/v4/letter/j/9de0a6/32.png) [@Josselin](https://rubytalk.org/u/Josselin)\
**Post date:** [29 January 2007 07:00 UTC](https://rubytalk.org/t/string-into-array/34871/5 "2007-01-29T07:00:11Z")

</div>

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

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

> **···**
>
> On 2007-01-28 11:20:27 +0100, Robert Klemme \<shortcutter@googlemail.com\> said:
> 
> > On 28.01.2007 11:13, Josselin wrote:
> > 
> > > 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
> > 
> > &nbsp;&nbsp;robert
