Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re^10: Processing CSV File

by Kenosis (Priest)
on Oct 05, 2012 at 05:11 UTC ( [id://997380]=note: print w/replies, xml ) Need Help??


in reply to Re^9: Processing CSV File
in thread Processing CSV File

Give the following change a try:

open my $csvfh, '<:crlf', $csvFile or die "Unable to open $csvFile: $! +"; ^ | + - Add the :crlf

Replies are listed 'Best First'.
Re^11: Processing CSV File
by Perl3r (Novice) on Oct 05, 2012 at 05:30 UTC

    Same error as before..

    Use of uninitialized value $vars in chomp at create-configs.pl line 18 +, <$csvfh> line 1. Use of uninitialized value $vars in split at create-configs.pl line 19 +, <$csvfh> line 1.

      OK... Instead of installing Text::CSV, you can download CSV.pm into the same directory as create-configs.pl. Here's the updated script that requires CSV.pm to parse the csv files:

      #!/usr/bin/perl # use strict; use warnings; require 'CSV.pm'; $ARGV[0] or die "Usage: $0 <filename> [<filename>] ..."; my ( $template_file_name, $templateText, %hash ) = ''; my $csv = Text::CSV->new( { binary => 1, eol => $/ } ) or die "Cannot use CSV: " . Text::CSV->error_diag(); for my $csvFile (@ARGV) { print "\nProcesing file $csvFile ...\n"; open my $csvfh, '<', $csvFile or die "Unable to open $csvFile: $!" +; # Ignore column names in first line my $columnNames = $csv->getline($csvfh); # Get vars from second line my @vars = @{ $csv->getline($csvfh) }; # Process each csv line while ( my $row = $csv->getline($csvfh) ) { my $templateFN = pop @$row; if ( $template_file_name ne $templateFN ) { $template_file_name = $templateFN; undef $templateText; } # keys: fields from second line; values: fields from csv line @hash{@vars} = @$row; $templateText //= getTemplateText($template_file_name); my $templateTextCopy = $templateText; $templateTextCopy =~ s/$_/$hash{$_}/g for keys %hash; my $ofile_name = $hash{'##rtrname##'} . '.txt'; print "Writing to file: $ofile_name\n"; open my $fh, '>', $ofile_name or die "$ofile_name: $!"; print $fh $templateTextCopy; close $fh; } close $csvfh; } print "\nDone!\n"; sub getTemplateText { my ($template_file_name) = @_; local $/; open my $fh, '<', $template_file_name or die "$template_file_name: + $!"; $templateText = <$fh>; close $fh; return $templateText; }

      This workaround saves you from installing the Text::CSV Module.

        • When using Text::CSV_XS or Text::CSV do not pass eol on parser objects when eol is withing default realms. That is all handled automatically.
        • Then "<:crnl" will not be required at all.
        • If needing CSV parsing and CSV writing, make twoCSV objects where only the writing object defines eol
        • Use auto_diag!
        # No eol and auto_diag > 1 will cause die's on error (with diagnostics +) my $csv = Text::CSV_XS->new ({ binary => 1, auto_diag => 2 });

        Enjoy, Have FUN! H.Merijn

        I get the following error

        Can't locate Text/CSV_PP.pm in @INC (@INC contains: /Library/Perl/5.12/darwin-thread-multi-2level ....
        I did the same thing for the CSV_PP.pm, as was done for the CSV.pm.. ie downloaded the code, and created a CSV_PP.pm in the same directory..
        Though no joy there..
        Any other ideas?
        Thanks

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://997380]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-04-19 02:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found