Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Re: Export CSV v2

by GrandFather (Sage)
on Oct 09, 2012 at 00:10 UTC ( #997899=note: print w/replies, xml ) Need Help??

in reply to Export CSV v2

You would get that error for an empty file or a file that has more than one blank line at the start. If you are running the script on *nix, but create the file under Windows then you may have a line end issue.

If you can't figure out the problem then you need to generate a script we can test. See I know what I mean. Why don't you? for hints on putting a sample script together that doesn't require separately provided external test files.

True laziness is hard work

Replies are listed 'Best First'.
Re^2: Export CSV v2
by Perl3r (Initiate) on Oct 09, 2012 at 00:15 UTC

    Thanks Grandfather..
    File was generated on a Mac OS X, and was edited in MS Excel. Code is being run on a Mac OS X aswell, but would like it to be able to run on any *nux system.
    The CSV file has no blank lines at the start, but i do believe it to be a line end issue, just not sure how to resolve..

      Try adding:

      $csv->eol ("\x0d\x0a");

      after the my $csv = Text::CSV_XS->new ... statement. Probably Excel is generating Windows line ends even on a Mac and Perl is expecting either *nix of (legacy) Mac line endings.

      True laziness is hard work

        Don't! Text::CSV_XS and Text::CSV both dynamically support \r \x0D, \n \x0A, or \r\n \x0D\x0A even if they change per line if eol is not specified.

        You might want to do so on output though:

        print "Writing to file: $ofile_name\n"; $templateTextCopy =~ s{(?: \r\n | \r | \n )} {\r\n}gx; # <-- make line ending consis +tent open my $fh, ">", $ofile_name or die "$ofile_name: $!"; print $fh $templateTextCopy; close $fh;

        Enjoy, Have FUN! H.Merijn

        Ok I have the script as follows:

        my $csv = Text::CSV->new( { binary => 1, auto_diag => 2, } ) or die "Cannot use CSV: " . Text::CSV->error_diag(); $csv->eol ("\x0d\x0a"); for my $csvFile (@ARGV) { print "\nProcesing file $csvFile ...\n"; open my $csvfh, '<', $csvFile or die "Unable to open $csvFile: $!" +;

        When I run the script, i still get the following error:

        Can't use an undefined value as an ARRAY reference at line 23, <$csvfh> line 1.

        This happens whether the CSV is in MS Excel format, or TXT

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://997899]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (6)
As of 2018-06-23 10:33 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (125 votes). Check out past polls.