# Need to convert string to array

**URL:** <https://www.ruby-forum.com/t/need-to-convert-string-to-array/126981>\
**Category:** Ruby\
**Created:** [January 23, 2008, 11:06pm UTC](https://www.ruby-forum.com/t/need-to-convert-string-to-array/126981 "2008-01-23T23:06:18Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![jackster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jackster](https://www.ruby-forum.com/u/jackster)\
**Post date:** [January 23, 2008, 11:06pm UTC](https://www.ruby-forum.com/t/need-to-convert-string-to-array/126981/1 "2008-01-23T23:06:18Z")

</div>

I have the following data stored in a string. This data is composed of  
many records.

This is a single record:

2008-01-22,88.34,98.56,86.68,96.54,2175600,96.54

This is the string:

“2008-01-22,88.34,98.56,86.68,96.54,2175600,96.542008-01-18,92.23,96.58,90.77,92.98,3812400,92.982008-01-17,96.37,97.30,86.84,89.36,3753200,89.362008-01-16,101.74,103.33,94.16,95.43,2902800,95.432008-01-15,98.92,105.25,98.51,103.28,1881900,103.282008-01-14,96.30,102.44,96.30,100.52,1113900,100.522008-01-11,101.56,104.70,94.16,96.24,1516300,96.242008-01-10,102.00,103.85,98.03,102.78,1853800,102.782008-01-09,101.87,103.50,97.42,102.85,1205300,102.852008-01-08,105.00,109.50,101.89,102.28,1563800,102.282008-01-07,108.76,109.00,99.50,105.18,1779400,105.182008-01-04,110.27,110.45,106.49,107.54,1386000,107.542008-01-03,115.71,116.00,110.63,111.40,1213500,111.402008-01-02,116.75,119.00,113.20,115.24,1619800,115.24  
\n”

I need to get the first element (2008-01-22) followed by the last  
element (96.54) out for each record so I graph them.

Does anyone know of a way to perform this task?

thanks  
jackster

---

<div class="post-metadata">

**Author:** ![jackster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jackster](https://www.ruby-forum.com/u/jackster)\
**Post date:** [January 23, 2008, 11:25pm UTC](https://www.ruby-forum.com/t/need-to-convert-string-to-array/126981/2 "2008-01-23T23:25:40Z")

</div>

On Jan 23, 3:13 pm, Phrogz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > 2008-01-22,88.34,98.56,86.68,96.54,2175600,96.54
> 
> str =  
> “2008-01-22,88.34,98.56,86.68,96.54,2175600,96.542008-01-18,92.23,96.58,90.77,92.98,3812400,92.982008-01-17,96.37,97.30,86.84,89.36,3753200,89.362008-01-16,101.74,103.33,94.16,95.43,2902800,95.432008-01-15,98.92,105.25,98.51,103.28,1881900,103.282008-01-14,96.30,102.44,96.30,100.52,1113900,100.522008-01-11,101.56,104.70,94.16,96.24,1516300,96.242008-01-10,102.00,103.85,98.03,102.78,1853800,102.782008-01-09,101.87,103.50,97.42,102.85,1205300,102.852008-01-08,105.00,109.50,101.89,102.28,1563800,102.282008-01-07,108.76,109.00,99.50,105.18,1779400,105.182008-01-04,110.27,110.45,106.49,107.54,1386000,107.542008-01-03,115.71,116.00,110.63,111.40,1213500,111.402008-01-02,116.75,119.00,113.20,115.24,1619800,115.24\n”
> 
> # Find a date, followed by characters that we don’t care about,
> 
> # followed by a comma, followed by one or more non commas,
> 
> # up until you see a date coming up
> 
> year\_and\_chunk = str.scan( /(\d{4}-\d{2}-\d{2}).+?,([^,]+)(?=\d{4}-  
> \d{2}-\d{2})/ )

Oops…as shown in the output, the above fails to catch the very last  
entry. Here’s a fix (and also doesn’t have the nonsensical comment the  
last one had):

str =  
“2008-01-22,88.34,98.56,86.68,96.54,2175600,96.542008-01-18,92.23,96.58,90.77,92.98,3812400,92.982008-01-17,96.37,97.30,86.84,89.36,3753200,89.362008-01-16,101.74,103.33,94.16,95.43,2902800,95.432008-01-15,98.92,105.25,98.51,103.28,1881900,103.282008-01-14,96.30,102.44,96.30,100.52,1113900,100.522008-01-11,101.56,104.70,94.16,96.24,1516300,96.242008-01-10,102.00,103.85,98.03,102.78,1853800,102.782008-01-09,101.87,103.50,97.42,102.85,1205300,102.852008-01-08,105.00,109.50,101.89,102.28,1563800,102.282008-01-07,108.76,109.00,99.50,105.18,1779400,105.182008-01-04,110.27,110.45,106.49,107.54,1386000,107.542008-01-03,115.71,116.00,110.63,111.40,1213500,111.402008-01-02,116.75,119.00,113.20,115.24,1619800,115.24\n”

# Find a date, followed by characters that we don’t care about,

# followed by a comma, followed by one or more non commas,

# up until you see a date coming up

year\_and\_chunk = str.scan( /(\d{4}-\d{2}-\d{2}).+?,([^,]+)(?=\d{4}-  
\d{2}-\d{2}|$)/ )

puts year\_and\_chunk.flatten  
#=\> 2008-01-22  
#=\> 96.54  
#=\> 2008-01-18  
#=\> 92.98  
#=\> 2008-01-17  
#=\> 89.36  
#=\> 2008-01-16  
#=\> 95.43  
#=\> 2008-01-15  
#=\> 103.28  
#=\> 2008-01-14  
#=\> 100.52  
#=\> 2008-01-11  
#=\> 96.24  
#=\> 2008-01-10  
#=\> 102.78  
#=\> 2008-01-09  
#=\> 102.85  
#=\> 2008-01-08  
#=\> 102.28  
#=\> 2008-01-07  
#=\> 105.18  
#=\> 2008-01-04  
#=\> 107.54  
#=\> 2008-01-03  
#=\> 111.40  
#=\> 2008-01-02  
#=\> 115.24

---

<div class="post-metadata">

**Author:** ![jackster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jackster](https://www.ruby-forum.com/u/jackster)\
**Post date:** [January 23, 2008, 11:16pm UTC](https://www.ruby-forum.com/t/need-to-convert-string-to-array/126981/3 "2008-01-23T23:16:54Z")

</div>

On Jan 23, 3:06 pm, jackster the jackle [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> \n"
> 
> I need to get the first element (2008-01-22) followed by the last  
> element (96.54) out for each record so I graph them.
> 
> Does anyone know of a way to perform this task?

str =  
“2008-01-22,88.34,98.56,86.68,96.54,2175600,96.542008-01-18,92.23,96.58,90.77,92.98,3812400,92.982008-01-17,96.37,97.30,86.84,89.36,3753200,89.362008-01-16,101.74,103.33,94.16,95.43,2902800,95.432008-01-15,98.92,105.25,98.51,103.28,1881900,103.282008-01-14,96.30,102.44,96.30,100.52,1113900,100.522008-01-11,101.56,104.70,94.16,96.24,1516300,96.242008-01-10,102.00,103.85,98.03,102.78,1853800,102.782008-01-09,101.87,103.50,97.42,102.85,1205300,102.852008-01-08,105.00,109.50,101.89,102.28,1563800,102.282008-01-07,108.76,109.00,99.50,105.18,1779400,105.182008-01-04,110.27,110.45,106.49,107.54,1386000,107.542008-01-03,115.71,116.00,110.63,111.40,1213500,111.402008-01-02,116.75,119.00,113.20,115.24,1619800,115.24\n”

# Find a date, followed by characters that we don’t care about,

# followed by a comma, followed by one or more non commas,

# up until you see a date coming up

year\_and\_chunk = str.scan( /(\d{4}-\d{2}-\d{2}).+?,([^,]+)(?=\d{4}-  
\d{2}-\d{2})/ )

# Throw out the first (empty) value (a result of the year being the

first entry)  
puts year\_and\_chunk.flatten  
#=\> 2008-01-22  
#=\> 96.54  
#=\> 2008-01-18  
#=\> 92.98  
#=\> 2008-01-17  
#=\> 89.36  
#=\> 2008-01-16  
#=\> 95.43  
#=\> 2008-01-15  
#=\> 103.28  
#=\> 2008-01-14  
#=\> 100.52  
#=\> 2008-01-11  
#=\> 96.24  
#=\> 2008-01-10  
#=\> 102.78  
#=\> 2008-01-09  
#=\> 102.85  
#=\> 2008-01-08  
#=\> 102.28  
#=\> 2008-01-07  
#=\> 105.18  
#=\> 2008-01-04  
#=\> 107.54  
#=\> 2008-01-03  
#=\> 111.40

---

<div class="post-metadata">

**Author:** ![jackster](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/bc8723/32.png) [@jackster](https://www.ruby-forum.com/u/jackster)\
**Post date:** [January 24, 2008, 12:26am UTC](https://www.ruby-forum.com/t/need-to-convert-string-to-array/126981/4 "2008-01-24T00:26:28Z")

</div>

whoa, Gavin…thanks!  
I didn’t know about the scan method…It looks like this will do  
exactly what I’m looking for. I will get on the server tomorrow morning  
and get er done.

thanks again

jackster.mobi

Gavin K. wrote:

> On Jan 23, 3:13 pm, Phrogz [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > > 2008-01-22,88.34,98.56,86.68,96.54,2175600,96.54
> > 
> > str =  
> > “2008-01-22,88.34,98.56,86.68,96.54,2175600,96.542008-01-18,92.23,96.58,90.77,92.98,3812400,92.982008-01-17,96.37,97.30,86.84,89.36,3753200,89.362008-01-16,101.74,103.33,94.16,95.43,2902800,95.432008-01-15,98.92,105.25,98.51,103.28,1881900,103.282008-01-14,96.30,102.44,96.30,100.52,1113900,100.522008-01-11,101.56,104.70,94.16,96.24,1516300,96.242008-01-10,102.00,103.85,98.03,102.78,1853800,102.782008-01-09,101.87,103.50,97.42,102.85,1205300,102.852008-01-08,105.00,109.50,101.89,102.28,1563800,102.282008-01-07,108.76,109.00,99.50,105.18,1779400,105.182008-01-04,110.27,110.45,106.49,107.54,1386000,107.542008-01-03,115.71,116.00,110.63,111.40,1213500,111.402008-01-02,116.75,119.00,113.20,115.24,1619800,115.24\n”
> > 
> > # Find a date, followed by characters that we don’t care about,
> > 
> > # followed by a comma, followed by one or more non commas,
> > 
> > # up until you see a date coming up
> > 
> > year\_and\_chunk = str.scan( /(\d{4}-\d{2}-\d{2}).+?,([^,]+)(?=\d{4}-  
> > \d{2}-\d{2})/ )
> 
> Oops…as shown in the output, the above fails to catch the very last  
> entry. Here’s a fix (and also doesn’t have the nonsensical comment the  
> last one had):
> 
> str =  
> “2008-01-22,88.34,98.56,86.68,96.54,2175600,96.542008-01-18,92.23,96.58,90.77,92.98,3812400,92.982008-01-17,96.37,97.30,86.84,89.36,3753200,89.362008-01-16,101.74,103.33,94.16,95.43,2902800,95.432008-01-15,98.92,105.25,98.51,103.28,1881900,103.282008-01-14,96.30,102.44,96.30,100.52,1113900,100.522008-01-11,101.56,104.70,94.16,96.24,1516300,96.242008-01-10,102.00,103.85,98.03,102.78,1853800,102.782008-01-09,101.87,103.50,97.42,102.85,1205300,102.852008-01-08,105.00,109.50,101.89,102.28,1563800,102.282008-01-07,108.76,109.00,99.50,105.18,1779400,105.182008-01-04,110.27,110.45,106.49,107.54,1386000,107.542008-01-03,115.71,116.00,110.63,111.40,1213500,111.402008-01-02,116.75,119.00,113.20,115.24,1619800,115.24\n”
> 
> # Find a date, followed by characters that we don’t care about,
> 
> # followed by a comma, followed by one or more non commas,
> 
> # up until you see a date coming up
> 
> year\_and\_chunk = str.scan( /(\d{4}-\d{2}-\d{2}).+?,([^,]+)(?=\d{4}-  
> \d{2}-\d{2}|$)/ )
> 
> puts year\_and\_chunk.flatten  
> #=\> 2008-01-22  
> #=\> 96.54  
> #=\> 2008-01-18  
> #=\> 92.98  
> #=\> 2008-01-17  
> #=\> 89.36  
> #=\> 2008-01-16  
> #=\> 95.43  
> #=\> 2008-01-15  
> #=\> 103.28  
> #=\> 2008-01-14  
> #=\> 100.52  
> #=\> 2008-01-11  
> #=\> 96.24  
> #=\> 2008-01-10  
> #=\> 102.78  
> #=\> 2008-01-09  
> #=\> 102.85  
> #=\> 2008-01-08  
> #=\> 102.28  
> #=\> 2008-01-07  
> #=\> 105.18  
> #=\> 2008-01-04  
> #=\> 107.54  
> #=\> 2008-01-03  
> #=\> 111.40  
> #=\> 2008-01-02  
> #=\> 115.24
