Reputation: 954
I am trying to parse a CSV file using perl, I need to read, add a new row (both into a empty file and to the file already consisting data) and delete the row/rows by matching a particular value, I wanted to do it using a inbuilt module only doesn't want to install a external new module.
Earlier I was trying it with using xml file but it required to install XML::Twig or XML::LibXML which I didn't want.
Is it possible through csv or text file.
Upvotes: 0
Views: 208
Reputation: 152
You can parse CSV file into XML without installing new module. Here is how:
use warnings;
use strict;
use Data::Dumper;
my $CSVfile="customer.csv";
print "Input Csv file: $CSVfile \n";
my ($arrRef,$keys)= convertCsvToHash($CSVfile);
print Dumper($arrRef);
sub convertCsvToHash{
my $file = shift;
open(FILE, "$file");
my @arr = <FILE>;
close(FILE);
my @hashArry;
chomp($arr[0]);
my @keys = split(',',$arr[0]);
for my $i(1..$#arr){
chomp($arr[$i]);
my @splitRec = split(',',$arr[$i]);
my $hash = {};
for my $j(0..$#keys){
$hash->{$keys[$j]} = $splitRec[$j]
}
push @hashArry,$hash;
}
return \@hashArry, \@keys;
}
Upvotes: 0
Reputation: 21666
You should use Text::CSV module. If you don't want to install it then you are left with split and tricky handling.
See this tutorial by Gabor: How to read a CSV file using Perl?
Upvotes: 1