Dear list,

I am trying to import entries in a csv file into a relational database, however there are entries such as:

a,b,c,d,e,"f1,f2","g1,g2" which spoil my split(/,/).

The quotes group f1 and f2 as a single entry. I know each each database entry should be atomic, but I'll deal with that later.

So I would like to change the above entry to

a,b,c,d,e,f1|f2,g1|g2

I had an inelegant solution:

   while ($line =~ /(".*?")/g){

      my $comma = my $noComma = $1;
      $noComma =~ s/,/|/g;
      $noComma =~ s/"//g;
      $line =~ s/$comma/$noComma/;

   }

But this had problems, when an entry is:

a,b,c,d,e,"f(1),f(2)","g(1),g(2)"

since the parentheses need to be escaped in the substitution. The data I'm working with is highly unpredictable, as illustrated by the above example. What is consistent are the commas and quotes.

I could probably come up with a solution but am afraid it will only become more and more ugly.

Could someone provide some guidance?

Thanks!

Dave


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to