Mamta Vyas Mamta Vyas - 7 months ago 23
Perl Question

How to find integer column in CSV file and also get sum of that column using Perl?

I am generating CSV files after reading records from database tables. To verify that the CSV files are being properly generated, I need to find the integer columns in CSV and calculate their sum to compare with database table.
I want to use Perl to accomplish this task as some of my CSV files may contain huge amount of records.
I am quite new to Perl, after searching I have applied following solutions:-


  1. For getting sum of integer values in column used the command given below:

    perl -e "map {$x += $_} <> and print $x" < filepath



But this command gives only sum of 1st column in my CSV file. But my csv file can have an integer column at any position.


  1. Then I tried following script:

    my $sum = 0;
    my $line = "111^ABC^XYZ^120000";
    chomp $line;
    my @fields = split /^/ , $line;
    $sum += $fields[0];
    print "$sum\n";



This script works fine when I replace ^(caret sign) with :(colon) etc,but my csv contains data separated by caret(^) which doesn't work.

So can anyone suggest me a solution in PERL which can find position of integer columns in my csv file and calculate sum of those columns or how can I make the above script work for ^(caret sign).

Answer

Caret is special in regular expressions: it matches the beginning of a line. To match literal caret, you need to backslash it:

split /\^/

For CSV manipulation, Text::CSV is usually better if your input contains quoted fields or escaped separators.

Comments