in reply to how to sum over rows based on column headings in perl
#!/usr/bin/perl # http://perlmonks.org/?node_id=1136784 use strict; use warnings; my (%counts, %unique, %masks); my ($headname, @unique) = grep !$unique{$_}++, my @headers = split ' ' +, <DATA>; for my $label ( @unique ) { $masks{$label} = join ' ', map { $_ eq $label ? 'M' : '_' } @headers[1..$#headers]; } while(<DATA>) { s/\s+/ /g; # for uniform spacing my ($name, $letters) = split ' ', $_, 2; $counts{$name}{$_} += ($masks{$_} | $letters) =~ /M/ for @unique; } print "$headname @unique\n"; print "$_ @{ $counts{$_} }{@unique}\n" for sort keys %counts; # Gname G1 G2 G3 # A 1 0 1 # B 1 2 2 # C 2 1 0 __DATA__ Gname G1 G1 G1 G1 G2 G2 G3 A W W M W W W M A W W W W W W W A W W W W W W W B W W W W W M M B M W W W W M M C M M M W W W W C M W W M M W W
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: how to sum over rows based on column headings in perl
by angerusso (Novice) on Jul 30, 2015 at 20:34 UTC | |
|
Re^2: how to sum over rows based on column headings in perl
by angerusso (Novice) on Jul 31, 2015 at 16:46 UTC | |
by 1nickt (Canon) on Jul 31, 2015 at 17:03 UTC | |
by angerusso (Novice) on Jul 31, 2015 at 19:07 UTC | |
by Anonymous Monk on Jul 31, 2015 at 19:29 UTC | |
by angerusso (Novice) on Jul 31, 2015 at 19:58 UTC | |
| |
by Anonymous Monk on Jul 31, 2015 at 17:12 UTC | |
by angerusso (Novice) on Jul 31, 2015 at 18:48 UTC |