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;
}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|