Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much

Re^9: Processing CSV File

by Anonymous Monk
on Oct 05, 2012 at 04:58 UTC ( #997376=note: print w/replies, xml ) Need Help??

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

Thanks for the reply Kenosis
I was trying to steer clear of installing modules, as the script could be used on different machines at any time all over the place, of which some I do not have control.. so was hoping to have a script that worked 'out of the box' on any machine.
Do you think there would be any way around it?
If there really is no other way, then perhaps I will need to explore that option.. but would prefer not to if at all possible

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

    Give the following change a try:

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

      Same error as before..

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

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

        #!/usr/bin/perl # use strict; use warnings; require ''; $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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://997376]
[marto]: good morning all
[Discipulus]: good morning marto! a better weekend? i hope

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2018-05-21 07:47 GMT
Find Nodes?
    Voting Booth?