in reply to Re^5: Processing CSV File
in thread Processing CSV File
Am glad it's working for you.
The script below addresses #1 and #3. Am unclear about #2. Where is that newline appearing? Here's the script:
#!/usr/bin/perl # use strict; use warnings; $ARGV[0] or die "Usage: $0 <filename> [<filename>] ..."; my ( $template_file_name, $templateText, %hash ) = ''; 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 = <$csvfh>; # Get vars from second line chomp( my $vars = <$csvfh> ); my @vars = split /,/, $vars; while (<$csvfh>) { chomp; my @fields = split /,/; my $templateFN = pop @fields; if ( $template_file_name ne $templateFN ) { $template_file_name = $templateFN; undef $templateText; } @hash{@vars} = @fields; $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; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^7: Processing CSV File
by Perl3r (Novice) on Oct 05, 2012 at 02:38 UTC | |
by Kenosis (Priest) on Oct 05, 2012 at 04:45 UTC | |
by Anonymous Monk on Oct 05, 2012 at 04:58 UTC | |
by Kenosis (Priest) on Oct 05, 2012 at 05:11 UTC | |
by Perl3r (Novice) on Oct 05, 2012 at 05:30 UTC | |
|