From: Bill Kelly Date: 2001-11-05T05:25:21+09:00 Subject: [ruby-talk:24349] Re: Joys of eval Hi Albert, From: "Albert Wagner" > > A few weeks ago I posted a request for help with regexp, split, scan, et. > al., in an attempt to separate a comma delimited string into its component > fields. I just stumbled upon an obvious but previously unseen solution to > the problem: > > x = '"abc","de,f",123,4.56,"ghi"' > y = eval('['< > y is now: ["abc", "de,f", 123, 4.56, "ghi"] > Just wondering if the regexp version had indeed worked out OK, or have you encountered data since then it couldn't handle? (The one in [ruby-talk:22534] didn't handle embedded quotes for instance; the one in [ruby-talk:22537] can handle embedded quotes, but, I suspect, probably not the way something like MSAccess might output them, given what was learned about Excel's seemingly strange way of quoting in [ruby-talk:22541] . . . :-) By the way I recently discovered that the flatten!.compact! used in the code referenced above should be changed to flatten.compact without the exclamation points. Or at least the 'flatten' at any rate - because the 'flatten!' version "returns nil if no modifications were made." (Pickaxe, Ch. 22) Whereas, we need the resulting array always, modified or not, to pass on to 'compact'. If you have need of handling embedded quotes in the CSV parsing, perhaps you could post some examples here of the quoting syntax MSAccess uses to encode the data? I'm pretty sure the regexp can be adapted to handle it. For instance, Excel had taken: +-----+---------------+ |blerb|"hey,you","man,| +-----+---------------+ And had written (to the .csv file): blerb,"""hey,you"",""man," . . . But, odd-looking or not, this appears well within the realm of regexps to handle. :-) Regards, Bill