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

Re^6: Processing CSV File

by Kenosis (Priest)
on Oct 05, 2012 at 01:13 UTC ( #997354=note: print w/replies, xml ) Need Help??

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 (Initiate) on Oct 05, 2012 at 02:38 UTC

    You deserve a medal! That works perfectly!
    The problem with #2 is;
    If i open the .CSV in MS Excel, make a change and save it, then when i run the .pl, i get the following error:

    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.

    However, if i open the .CSV in excel, make the changes, then open it with something like Text Edit, and go to the end of each line in the CSV, and press delete, then enter (to put evreything back to its own line), then when I run the .pl it works perfectly..
    Does that make sense? if not, let me know, and I will try and send you the files if possible, so you can know what i mean..
    Once again, your help is invaluable..

      Are you able to install Perl Modules? If so, install Text::CSV, and let me know when this is done.

      There are two reasons that the Module may serve you well in this case. The first is that it's generally a best practice to use a Module that's built to handle csv files. In your case, the csv format wasn't complex, i.e., commas within quoted fields, so just splitting on commas was OK. However, thinking long-term, your data may get more complex, and Text::CSV would be able to handle it.

      The second reason is based upon the error messages you shared. The variable $vars was uninitialized when it should have contained a value. This may be an issue of the csv file containing binary data, but I'm not sure. Text::CSV can handle this, too.

      Let me know...

        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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://997354]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (2)
As of 2018-01-24 00:39 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (254 votes). Check out past polls.