From: jackster the jackle Date: 2008-01-24T08:26:22+09:00 Subject: Re: Need to convert string to array 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 Kistner wrote: > On Jan 23, 3:13 pm, Phrogz 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 -- Posted via http://www.ruby-forum.com/.