Ruprecht Helms wrote:
> Hi,
>
> how can I parse a csv-file where the entries are seperated with  | .
> The scripts later should put them into a mysql-database using dbi.
>
> Especially for me is interessting how to parse the content of the file
> and store them into different variables for later processing.
> A scriptexample would be nice.

Most of the replies have suggested using 'split( /\|/, $line )'.
However, this ignores a potentially important aspect of common cvs
file formats - well, important to me, anyway - which is the
interaction between quotes, field delimiters, and newlines:

  "Harry|Sally" | Sleepless
  Jack|"Jill
  ""Walker"""

_should_ parse into two records of two fields each:

  record 1: (2 fields)
    Harry|Sally
    Sleepless
  record 2: (2 fields)
    Jack
    Jill\n"Walker"

If I merely split lines on /\|/, I get:

  record 1: (3 fields)
    "Harry
    Sally"
    Sleepless
  record 2: (2 fields)
    Jack
    "Jill
  record 3: (1 field)
    ""Walker"""

I need this so that I can use csv files generated by spreadsheet
applications such as OpenOffice.org.

-- 
Jonathan "Dataweaver" Lang

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to