in reply to Re: Processing CSV File
in thread Processing CSV File
If I may suggest considering a slightly different approach to your task...
It appears that you're opening the template file each time a csv line is read, so that you can fill in the template with the entries. Consider opening the template file only once, and grabbing its contents into a scalar. Then, you can do the substitutions on a copy of that scalar, and write that copy to a file. Doing that may look something like this:
#!/usr/bin/perl # use strict; use warnings; my ( $templateText, %hash ); my $template_file_name = 'rtr-template.txt'; my @vars = split "\n", <<END; ##location## ##rtrname## ##loop0-ip## ##frame-ip## ##frame-DLCI## ##eth0-ip## END { local $/; open my $fh, '<', $template_file_name or die "$template_file_name: + $!"; $templateText = <$fh>; close $fh; } while (<>) { chomp; @hash{@vars} = split /,/; my $templateTextCopy = $templateText; $templateTextCopy =~ s/$_/$hash{$_}/g for keys %hash; my $ofile_name = $hash{'##rtrname##'} . '.txt'; open my $fh, '>', $ofile_name or die "$ofile_name: $!"; print $fh $templateTextCopy; close $fh; }
You'll notice this notation: @hash{@vars} = split /,/; It pairs the elements of @var as keys with the split entries of the csv line as values of %hash. This creates the hash that's used in the substitution line, taking the place of the six substitution lines. Also, note that the substitution is global, because your script was going through all the lines of the template for the substitutions.
Hope this helps!
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: Processing CSV File
by Perl3r (Novice) on Oct 04, 2012 at 00:21 UTC | |
by Kenosis (Priest) on Oct 04, 2012 at 18:09 UTC | |
by Anonymous Monk on Oct 05, 2012 at 00:44 UTC | |
by Kenosis (Priest) on Oct 05, 2012 at 01:13 UTC | |
by Perl3r (Novice) on Oct 05, 2012 at 02:38 UTC | |
|