I am writing a Perl script and using the Class::CSV module. I have an array of numbers, 5 elements long
$values[0] - $values[4]
I am trying to add a new line to the CSV file and populate the new line with the values from the array. However I keep receiving this error when I try to run the script:
Failed to create CSV line from line:
10252205
at /usr/lib/perl5/site_perl/5.8.8/Class/CSV.pm line 257
Class::CSV::Line::string('Class::CSV::Line=HASH(0x1f2d0f20)') called at /usr/lib/perl5/site_perl/5.8.8/Class/CSV.pm line 435
Class::CSV::string('Class::CSV=HASH(0x1f2c1c00)') called at catchmailstats.pl line 116
Here is the code for the CSV construction:
# Create csv file from the current data
my $csv = Class::CSV->new(
fields => [qw/Month NotSpam Probable Quarantine Spam Total/],
);
# Creates the first row (Headers)
$csv->add_line({
Month => 'Month',
NotSpam => 'NotSpam',
Probable => 'Probable',
Quarantine => 'Quarantine',
Spam => 'Spam',
Total => 'Total'
});
# Creates the second row (values)
$csv->add_line([$values[0], $values[1],$values[2],$values[3],$values[4], $total]);
I have also tried it using the other notation style:
# Creates the second row (values)
$csv->add_line({
Month => $values[0],
NotSpam => $values[1],
Probable => $values[2],
Quarantine => $values[3],
Spam => $values[4],
Total => $total
});
Neither style has worked. Here is something I have noticed, though. The final variable, $total, is the sum of $values[1] through $values[4]. This variable will work just fine.
So my real question is: why won’t references to elements in my array work, but a summation of elements from the same array, compile without a problem? And how can I get the add_line function to accept my array elements?
I think that in your values list, there’s a
HASH REFthat needs to be dereferenced.Use
Data::Dumperin your code :And POST the output, we will see.