[GLLUG] Digests of CSV files

Martin A. Brooks martin at hinterlands.org
Fri Jan 5 19:39:37 UTC 2018


On 2018-01-05 18:11, John Levin via GLLUG wrote:
>  What I want is a
> program or script that will simply digest each column and relate them
> to each other, producing something along the lines of:
> There are 200 weavers.
> There are 190 male weavers.
> There are 20 weavers in Norwich Castle.
> There are 18 male weavers in Norwich Castle.
> etc.


Here's a bit of Perl to get you started:

--------------------
#!/usr/bin/perl


use strict;
use warnings;
use Data::Dumper;

my %stats;

while(<>){
     chomp;
     my ($g,$p,$t)=split/,/;
     $stats{gender}{$g}{$p}++;
     $stats{gender}{$p}{$g}++;
     $stats{trade}{$g}{$t}++;
     $stats{trade}{$t}{$p}++;
     $stats{prison}{$g}++;
     $stats{prison}{$t}++;
}

print Dumper %stats;
--------------------

Given some data like:

--------------------
m,A,tailor
f,B,tinker
m,A,soldier
f,C,spy
f,A,tinker
m,D,tailor
f,A,soldier
m,A,spy
f,A,spy
--------------------


You get....

--------------------
$ ./stats.pl < data
$VAR1 = 'prison';
$VAR2 = {
           'f' => 5,
           'tinker' => 2,
           'tailor' => 2,
           'spy' => 3,
           'm' => 4,
           'soldier' => 2
         };
$VAR3 = 'gender';
$VAR4 = {
           'm' => {
                    'D' => 1,
                    'A' => 3
                  },
           'A' => {
                    'm' => 3,
                    'f' => 3
                  },
           'B' => {
                    'f' => 1
                  },
           'D' => {
                    'm' => 1
                  },
           'C' => {
                    'f' => 1
                  },
           'f' => {
                    'B' => 1,
                    'A' => 3,
                    'C' => 1
                  }
         };
$VAR5 = 'trade';
$VAR6 = {
           'tinker' => {
                         'B' => 1,
                         'A' => 1
                       },
           'f' => {
                    'tinker' => 2,
                    'spy' => 2,
                    'soldier' => 1
                  },
           'spy' => {
                      'A' => 2,
                      'C' => 1
                    },
           'm' => {
                    'spy' => 1,
                    'tailor' => 2,
                    'soldier' => 1
                  },
           'tailor' => {
                         'D' => 1,
                         'A' => 1
                       },
           'soldier' => {
                          'A' => 2
                        }
         };
--------------------



More information about the GLLUG mailing list