http://www.perlmonks.org?node_id=997000


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

    Thanks, This is a great version of the code.. One question for you.. How would I change the "my $template_file_name = 'rtr-template.txt'" line to chose a template that is specified in the CSV.. rather than a file saved on the HDD.. So if I just added another column to the CSV.. this would make the script a bit more dynamic for different templates.. Thanks!

      Hi, Perl3r.

      Try the following:

      #!/usr/bin/perl # use strict; use warnings; $ARGV[0] or die "Usage: $0 <filename> [<filename>] ..."; my ( $template_file_name, $templateText, %hash ) = ''; my @vars = split "\n", <<END; ##location## ##rtrname## ##loop0-ip## ##frame-ip## ##frame-DLCI## ##eth0-ip## END while (<>) { 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'; open my $fh, '>', $ofile_name or die "$ofile_name: $!"; print $fh $templateTextCopy; close $fh; } sub getTemplateText { my ($template_file_name) = @_; local $/; open my $fh, '<', $template_file_name or die "$template_file_name: + $!"; $templateText = <$fh>; close $fh; return $templateText; }

      The last CSV column needs to contain the template's file name. The script can handle multiple files at once, e.g.:

      processCSV.pl csv1.csv csv2.csv

      The script uses Perl's 'defined-or-equals' operator to only read the template's text once, and will read in a new template if it detects a change in the template's file name (e.g., when going from csv1.csv to csv2.csv).

        Thanks Kenosis!! Your help is invaluable.. Ok my .pl as it stands is as follows;

        #!/usr/bin/perl # use strict; use warnings; $ARGV[0] or die "Usage: $0 <filename> [<filename>] ..."; my ( $template_file_name, $templateText, %hash ) = ''; my @vars = split "\n", <<END; ##location## ##rtrname## ##loop0-ip## ##ospf-id## ##ospf-area## ##ospf-network## ##ospf-mask## ##eth00-ip## ##eth00-sm## END while (<>) { 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 = './Config_Output/' . $hash{'##rtrname##'} . '.txt +'; open my $fh, '>', $ofile_name or die "$ofile_name: $!"; print $fh $templateTextCopy; close $fh; } sub getTemplateText { my ($template_file_name) = @_; local $/; open my $fh, '<', './Templates/' . $template_file_name . '.txt' or + die "$template_file_name: $!"; $templateText = <$fh>; close $fh; return $templateText; }

        I still have a couple of problems, I was hoping you may be able to help with..
        1. Is there a way that I can ignore the first line of the CSV? - In the CSV the first line is just column descriptions, and is not needed for the output..
        2. I can still not seem to strip the newline off the end of the CSV.. I have tried to replace chomp

        with

        s/\r\n//

        but that doesn't seem to do it.. unfortunately the csv needs to be edited with MS Excel, so when saving that, it adds its extra MS stuff to the file
        3. Even better - but i don't know if this is even possible.. would there be a way, so that rather than specifying all the template variables (i.e. ##eth00-ip##, ##location## etc), I could have them all listed on the second line of the CSV, and then the code can draw those values out...?

        I really appreciate your assistance, and will definitely have to buy you a beer!!