Hi Albert,

From: "Albert Wagner" <alwagner / tcac.net>
>
> Thank you, Robert.  I was unaware of both the csv module and the Ruby 
> Cookbook.  I got this far with String#split but couldn't get rid of the quote 
> in element 0 and how to force empty strings for the trailing commas:
> 
> irb(main):048:0> x
> "\"aaa\",,\"ccc\",,,\"fff\",,,"
> irb(main):049:0> y = x.split(/\"\s*,\s*\"|\"\s*,|\s*,\s*\"|\s*,|\s*\z/)
> ["\"aaa", "", "ccc", "", "", "fff"

You can keep the trailing empty fields by passing a 'negative limit'
into split, e.g. y = x.split(/regexp/, -1).  However, the elimination
of the leading quote on the first field seemed like a stumper (without
producing an unwanted initial empty field.)  Here's a regexp solution
using String#scan . . .

require 'runit/testcase'
require 'runit/cui/testrunner'

def csv_split(str)
    str.scan(/(?:\A|,)\s*"([^"]*)"|(?:\A|,)([^",]*)(?=,|\z)/).flatten!.compact!
end

class TestCsvSplit < RUNIT::TestCase
    def testCsvSplit
        fields = csv_split(%Q{"aaa",,"c,d,",,,"fff",,,})
        assert fields == ["aaa", "", "c,d,", "", "", "fff", "", "", ""]
    end
end

RUNIT::CUI::TestRunner.run(TestCsvSplit.suite)


Hope this covers all the cases.  :-)  BTW this doesn't handle
"embedded quotes" within a quoted string.  That is, the example
you provided didn't specify how or whether embedded quotes 
within quoted strings in the CSV fields were treated.  This
regexp (above) won't deal with quotes [escaped somehow] inside
a quoted string.

Hope this helps,

Bill