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

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**

Replies are listed 'Best First'.
Re^3: Appending data to large files
by kyle (Abbot) on Jan 13, 2009 at 17:45 UTC

    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 *

        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.