You may want to look at the Text::CSV perl module. It
already has all the logic built into it to handle CSV (Comma Separated Value)
files automatically. If you want to access the file as
an SQL database, take a look at DBI in conjunction with DBD::CSV. | [reply] |
You may also want to take a look at Text::ParseWords, it knows
how to split taking quotes into account, preserving escaped
characters, etc.
| [reply] |
Use something like /\",\"/ for your regexp. Be careful, though... presumably your string looks like '"alpha","beta","gamma"'. Using split will leave the leading and trailing double quote on the first and last elements, respectively. For example:
#!/usr/local/bin/perl
my @items = split /\",\"/, '"alpha","beta","gamma"';
foreach (@items) {
print "$_\n";
}
will spit out
"alpha
beta
gamma"
Just make sure you deal with those at some point; I'd probably do something like substr $string, 1, -1 If there's extra garbage in the string, maybe like <CODE>s/\"(.*)\"/$1/</CODE | [reply] [d/l] [select] |
Or...
<code>
$string = "\"Foo\",\"Bar\",\"Baz\"";
# Split by comma instead...
@words = split(/,/, $sting);
# ..and then remove the quotes
foreach $word (@words) {
$word =~ s/"//sg;
}
| [reply] |
But that requires a change to the string, in which case you would just make the string more straight forward with a simpler separator.
| [reply] |
grep {tr/\"//d, print $_ . ' '}( split /\",\"/, q["Just","Another","Perl","Hacker"] )
| [reply] [d/l] |
Taken from "perl cookbook",
use Text::ParseWords;
sub parse_csv {
return quotewords(",",0, $_[0];
} | [reply] |
@new_array = split(/,",/, $line_you_want_to_split);
$line_you_want_to_split would of course be a scalar that contains the,,,line you want to split up ;)
@new_array will be the,,, new array that's created. | [reply] |