From dd90d8b93d981a322ab094412f0dfdc53e4fe125 Mon Sep 17 00:00:00 2001
From: Gerhard Gonter <ggonter@gmail.com>
Date: Thu, 26 Apr 2018 14:23:03 +0200
Subject: [PATCH] simple module to produce tsv files

---
 modules/util/Util/tsv.pm | 68 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 68 insertions(+)
 create mode 100644 modules/util/Util/tsv.pm

diff --git a/modules/util/Util/tsv.pm b/modules/util/Util/tsv.pm
new file mode 100644
index 0000000..5ee5ecf
--- /dev/null
+++ b/modules/util/Util/tsv.pm
@@ -0,0 +1,68 @@
+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;
+
+
-- 
GitLab