in reply to Appending data to large files

I'd use Text::CSV_XS to read the CSV file. I'd read the file you're trying to change a little at a time, looking for the delimiter, and insert the CSV records when I find it.

my $record_end = qq{\$\$\$\$\n}; while (<DATAFILE>) { next if ( $_ ne $record_end ); # insert stuff from CSV here } continue { print OUT; }

You don't have to read all of any file at once or even mess around with regular expressions.

Replies are listed 'Best First'.
Re^2: Appending data to large files
by joec_ (Scribe) on Jan 13, 2009 at 17:24 UTC
    Hi, when i try to use Text::CSV i get the following error: Can't locate auto/Text/CSV/getline.al in @INC

    although if i do $csv->version it works and displays the version so the module is installed correctly - any ideas?Thanks

    **UPDATE: I installed Text::CSV 1.10 and Text::CSV_XS 0.58 and now with this code:

    $file = 'project_data.csv'; my $parser = Text::CSV->new(); while (my $row = $parser->getline($file)){ @fields = @$row; }

    I get the following error:

    Can't locate object method "getline" via package "project_data.csv" (perhaps you forgot to load "project_data.csv"?) at ./join_csv.pl line 25.

    Any ideas? Thanks

    **END**

      The getline method is expecting an object with a getline method. Basically you have to open the file and pass in the handle instead of the filename.

      my $csv_filename = 'project_data.csv'; open my $csv_fh, '<', $csv_filename or die "Can't read '$csv_filename': $!"; my $parser = Text::CSV->new(); while ( my $row = $parser->getline( $csv_fh ) ) { # work, work, work } close $csv_fh or die "close() failed: $!";
        Hi
        How do i go about inserting stuff from the csv, between END and $$$$? In the format:

        END
        > <Column 1>
        Data 1
        > <Column 2>
        Data 2
        $$$$

        Etc.

        In your example Kyle, 'insert csv stuff here' puts it after the $$$$ and i need it before.

        Thanks.

        * UPDATE *

        Since my CSV is autogenerated at runtime, i do not know how many columns there will be, is there any way of using Text::CSV to extract a 'header' row?

        TIA.
        * END *