in reply to Re^2: Appending data to large files
in thread Appending data to large files

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: $!";

Replies are listed 'Best First'.
Re^4: Appending data to large files
by joec_ (Scribe) on Jan 14, 2009 at 09:37 UTC
    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 *

      Here's a self-contained runnable version of the code I posted in Re: Appending data to large files.

      my $record_end = qq{\$\$\$\$\n}; while (<DATA>) { next if ( $_ ne $record_end ); print "--CSV stuff--\n"; } continue { print; } __DATA__ example 1 example 2 END $$$$ example 3 example 4 END $$$$

      Here's the output:

      example 1 example 2 END --CSV stuff-- $$$$ example 3 example 4 END --CSV stuff-- $$$$

      Unless I've misunderstood your requirements, this is putting the CSV stuff right where you want it.

      Integrating that with the CSV-handling code I posted in Re^3: Appending data to large files (and the rest of your program, of course) is up to you.

      As for your question about headers, a quick look at the Text::CSV_XS documentation reveals a column_names method. That's what I'd try first. If that doesn't work out, then maybe a longer quick look would be required.