I've below code which is used to read a csv file and convert to hash. The Keys are dependent on the number of key columns that user need.
use warnings;
use strict;
my %hash;
my $KeyCols = 2;
while (<DATA>) {
chomp;
my @cols = split /,/, $_, $KeyCols+1;
next unless @cols > $KeyCols;
my $v = pop @cols;
my $k = join '', @cols;
$hash{$k} = $v;
}
I need help in achieving the same logic using TEXT::CSV_XS package for efficiency. Please help.
Text::CSV_XSis going to do for you is thesplit ...line, but correctly regardless of the many possible "funny" cases, and perhaps faster --- what you need is the most basic use of it, and once you have@colsthen you can do your thing with it any way you would. So just open the docs, look at synopsis, and try it out. (Then there's many many posts about it here as well)