in reply to Exporting from flatfile database

The easiest way would be to change the "|"s to ","s. Then, save the file with a .csv extension. The file should open in MS Excel with no additional changes. The following code should do the conversion if you're not sure how to do it:

#!/usr/bin/perl -w use strict; open IN, "infile.dat"; open OUT, ">data.csv"; while (<IN>) { chomp $_; my @fields = split(/|/, $_); my $newLine = join(",", @fields); print OUT "$newLine\n" } close IN; close OUT;

Replies are listed 'Best First'.
Re: Re: Exporting from flatfile database
by grep (Monsignor) on Dec 26, 2001 at 11:34 UTC
    Your script:
    open IN, "infile.dat"; open OUT, ">data.csv"; while (<IN>) { chomp $_; my @fields = split(/|/, $_); my $newLine = join(",", @fields); print OUT "$newLine\n" } close IN; close OUT;
    would corrupt the data with:
    hi|I'm with GNU,Inc.|I need a comma in my title this|will|produce|,|to|many|fields
    Using pipes to delimit data is a better idea (usually) because it is less common in data. There are also several modules to parse CSV. Incuding Text::CSV.

    UPDATE: tilly has pointed out that Text::CSV_XS is a better solution than Text::CSV. I handles imbedded line endings in the data.

    grep
    grep> cd pub 
    grep> more beer
    
      Hi, i am new to perl. Recently, i have encounter a problem and i need some advise from u guys. Hope u can help me. Anyone know how to export a .csv file into excel using perl ??? I can only write a prog. to enter some data into excel but cant seem to call out a .csv file into excel using perl. Thks in advance.