My requirement is to read all.csv files in the format groupid_groupname.csv in a folder. Secondly after reading create two new columns with groupid & groupname and print the constant value fetched above.
I have written the code but while using just print it works perfect and shows exact result but when writing to csv file..it goes for a toss. Please advice
use strict;
use warnings;
use Text::CSV;
use Text::CSV_XS;
use autodie;
print_Filenames_directory();
sub print_Filenames_directory
{
my @files = <*.csv>;
foreach my $file (@files)
{
open my $in, "+<:encoding(utf8)", $file or die $!;
my $csv = Text::CSV_XS->new ({ binary => 1, auto_diag => 1 });
my @name = split _,$file;
my $grpid = $name[0];
my $grpname = substr($name[1], 0,-4);
my @headers;
while (my $row = $csv->getline($in))
{
if($.==1)
{
# Add value to @columns array
splice @$row, 0, 0, "Group ID";
splice @$row, 1, 0, "Group Name";
@headers = "@$row\n";
print "@headers";
#$csv->print(@headers);
}
else
{
splice @$row, 0, 0, $grpid;
splice @$row, 1, 1, $grpname;
print "@$row\n";
#$csv->print($in, @$row);
+
}
}
# check for errors
$csv->eof or $csv->error_diag();
close $in;
}
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.