Skip to content
Snippets Groups Projects
Commit dd90d8b9 authored by Gerhard Gonter's avatar Gerhard Gonter :speech_balloon:
Browse files

simple module to produce tsv files

parent 70de2e9d
Branches
No related tags found
No related merge requests found
package Util::tsv;
use strict;
sub new
{
my $class= shift;
my $label= shift;
my $columns= shift;
my $obj= { label => $label, cols => $columns, rows => [] };
bless $obj, $class;
$obj;
}
sub add_items
{
my $self= shift;
my $list= shift;
my @tsv_cols= @{$self->{cols}};
my ($rows, $label)= map { $self->{$_} } qw(rows label);
=begin comment
print <<"EOX";
h2. $label
EOX
print "|_. ", join (" |_. ", @tsv_cols), "|\n";
=end comment
=cut
foreach my $rec (@$list)
{
my %rec= map { $_ => $rec->{$_} } @tsv_cols;
# print "| ", join (" | ", map { $rec{$_} } @tsv_cols), "|\n";
push (@$rows, \%rec);
}
print "$label count: ", scalar (@$list), "\n";
# print "$label: ", main::Dumper ($list);
}
sub save_tsv
{
my $tsv_data= shift;
my $tsv_name= shift;
# print "tsv_data: ", main::Dumper ($tsv_data);
if (open (TSV, '>:utf8', $tsv_name)) # TODO: otherwise complain
{
my @cols= @{$tsv_data->{cols}};
print TSV join ("\t", @cols), "\n";
foreach my $row (@{$tsv_data->{rows}})
{
print TSV join ("\t", map { $row->{$_} } @cols), "\n";
}
close (TSV);
}
}
1;
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment