Tuesday, July 8, 2008

Read data from a csv file using perl (when the column count is not known)

I need to read a csv file, and process each and every column data (i do not know the column count before processing the file)

#!/usr/local/bin/perl

if (scalar(@ARGV) < 1)
{
print "Please Specify the file name to be read \n";
exit
}

$file = $ARGV[0];

open FILE, "< $file" or die "Can't open test.csv : $!";
while(<FILE>){
chop;
my(@columns)= split("\t");
my($colnum)=$#columns;
# print "Number of Column in this row is : $colnum\n";
foreach $col(@columns){
print "$col \t";
}
print "\n";
}
close FILE;

No comments: