Printing a CSV Header Using Tie :: Handle :: CSV

I wrote a perl script using Tie :: Handle :: CSV to process the dataset in the csv file and printed only what I needed into a new csv file. Right now I am printing a title bar with all field names, just using hardcoding like this:

print '"TERM", "STUDENT ID", "NAME", ..."'."\n"; 

I suspect this is a dumb way to do this, but I don't know how to access the header from the Tie :: Handle :: CSV object. It is created in such a way

 my $fh = Tie::Handle::CSV->new($file,header=> 1); 

and access to data is the same

 $line -> {'CATALOG_NBR'} 

I know enough to know that this is a hash link, but not enough to know how to print the header using this, and not hard code it. Obviously, β€œthey” usually change the exact column names and arrange them right after I get the script working with each term again.

Thanks for the help! Ja

+6
perl
source share
4 answers

If the title is defined, and not an array reference, then the first line of the document will be used as the title. Unfortunately, there is no way to get the header as it was originally, so it does not read the first line using the header parameter as false, saving it (it is already a ref array) and closing the file and re-reading again, I don’t see an easier way to do this is.

 use strict; use warnings; use Tie::Handle::CSV; use Data::Dumper; my $csv_fh = Tie::Handle::CSV->new('basic.csv', header => 0); my $header = <$csv_fh>; close $csv_fh; print join(',', @$header), "\n"; $csv_fh = Tie::Handle::CSV->new('basic.csv', header => 1); while (my $csv_line = <$csv_fh>) { # $csv_line can be used here ... } close $csv_fh; 
+3
source share

A bit late for the party, but I just released a new version of Tie::Handle::CSV (should depend on CPAN in a few hours). It adds support for the header() method, which returns a CSV-formatted header. Usage may look like this:

  my $fh = Tie::Handle::CSV->new( $file, header => 1 ); print $fh->header; 

Hope this helps,

  • danboo
+6
source share

According to the documentation , it looks like turning off header => 1 in your constructor will cause it to not parse the header. Then you should get the header column names as the first $line returned from the object.

It also means that your $line will be array references, not hash links, so you will need to print positional values ​​using $line->[0] , $line->[1] , etc. instead of using header names.

If you want to use the header => 1 parameter, you can get the hash keys with my @headers = keys %{ $line } . Please note that there is no guarantee that the keys will be in any particular order, so you will have to figure out how to order them yourself.

I really have not used this module, so this is all an assumption based on reading the documentation for the header parameter.

+2
source share

Thanks guys, I ended up making essentially a combination of your tips. I opened two file descriptors - the second is $ hfh with header => 0 so that I can print the header labels in the correct order:

 my $fh = Tie::Handle::CSV->new($file, header => 1); my $hfh = Tie::Handle::CSV->new($file, header=>0); my $line = <$hfh>; my $header; foreach(@{$line}) { $header .= "\"$_\","; } print "$header\n"; 

Thanks again for your help!

+1
source share

All Articles