Search for, and remove column from CSV file
I'm trying to write a subroutine that will take two arguments, a filename
and the column name inside a CSV file. The subroutine will search for the
second argument (column name) and remove that column (or columns) from the
CSV file and then return the CSV file with the arguments removed.
I feel like I've gotten through the first half of this sub (opening the
file, retrieve the headers and values) but I can't seem to find a way to
search the CSV file for the string that the user inputs and delete that
whole column. Any ideas? Here's what I have so far.
sub remove_columns {
my @Para = @_;
my $args = @Para;
die "Insufficent arguments\n" if ($nargs < 2);
open file, $file
$header = <file>;
chomp $header;
my @hdr = split ',',$header;
while (my $line = <file>){
chomp $line;
my @vals = split ',',$line;
#hash that will allow me to access column name and values quickly
my %h;
for (my $i=0; $i<=$#hdr;$i++){
$h{$hdr[$i]}=$i;
}
....
}
Here's where the search and removal will be done. I've been thinking about
how to go about this; the CSV files that I'll be modifying will be huge,
so speed is a factor, but I can't seem to think of a good way to go about
this. I'm new to Perl, so I'm struggling a bit.
No comments:
Post a Comment