Gossamer Forum
Home : General : Perl Programming :

CSV, but commas in some of the fields?

Quote Reply
CSV, but commas in some of the fields?
This one is really getting on my nerves. The whole point in a CSV is that it uses a unique seperator field.

However, with one that I am working on now, they have commas for the delimiter, and commas in some of the fields.

Does anyone have any ideas on how I could split this string up correctly?

CatalogueCity.co.uk,http://cj.uk.catalogcity.com,12/02/2003 07:49:27 PM,"Hat, The",N/A,"All the animals laugh at Hedgie the hedgehog when a sock becomes caught on his prickles, but it is Hedgie who has the last laugh since it is wintertime and it is keeping him warm. Illustrated in colour by the author.",7732870,Country Book Shop,,,,GBP,0.00,4.50,0.00,yes,http://www.qksrv.net/click-661133-10280697?url=http%3A%2F%2Famos.catalogcity.com%2Fcc.class%2Fcc%3Fpcd%3D5427519%26ccsyn%3D43,http://www.qksrv.net/image-661133-10280697,http://altura.speedera.net/ccimg.catalogcity.com/210000/211200/211247/Products/5427519.jpg,Books/Children's & Teen's Books/Children's Diaries & Journals,,,,,,,,,,,,,,,

I'm stumped Frown

TIA

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] CSV, but commas in some of the fields? In reply to
Here's a solution for you, maybe not the most elegant, but it works.
Code:
#!/usr/bin/perl

use strict;

my $string = q{--your string here--};

$string =~ s{(".*?),(.*?")}{$1<comma>$3}g;
my (@parts) = split /,/, $string;
for (@parts) {
s{<comma>}{,}g;
print $_, "\n";
}

Ivan
-----
Iyengar Yoga Resources / GT Plugins
Quote Reply
Re: [yogi] CSV, but commas in some of the fields? In reply to
Your a star... thanks :)

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [yogi] CSV, but commas in some of the fields? In reply to
<puzzled> - where do you capture the value of $3?
Quote Reply
Re: [Coombes] CSV, but commas in some of the fields? In reply to
I have my perl compiled such that it only assigns odd numbers to the numbered variables (i.e. $1, $3) in regexes. There's no particular purpose to this, it mainly serves to confuse others...Wink

Ivan
-----
Iyengar Yoga Resources / GT Plugins