Skip to content

Use with Text::CSV

shinobi edited this page Mar 5, 2018 · 10 revisions

Text::CSV

If you need to use Text::CSV, the rowset constructor will help you on this.

use Text::CSV;
use Data::StaticTable;
my $t1 := Data::StaticTable.new( csv(in => "myfile.csv") ):data-has-header;

You will probably have to use :data-has-header. Just make sure that the first line in the csv has this header. Depending on how your csv is constructed, you might lose data if, for example, you have rows with more elements that the ones in your header. If you want to recover them, change the code to this:

use Text::CSV;
use Data::StaticTable;
my %rejected;
my $t1 := Data::StaticTable.new( csv(in => "myfile.csv"), rejected-data => %rejected ):data-has-header;

The %rejected hash will have the row number where the data was discarded as the key, and the value will contain an array with all the rejected elements for that row.

Clone this wiki locally