I saw Not_a_Number's solution as I was about to post mine. I've added his extra test data plus an additional one of my own which has nothing in either column 2 or 3.
#!/usr/bin/env perl
use 5.010;
use strict;
use warnings;
my %result;
while (<DATA>) {
map {
$result{$_->[0]}[0] += defined $_->[1] && $_->[1] eq q{1_x} ?
+1 : 0;
$result{$_->[0]}[1] += defined $_->[2] ? 1 : 0;
} ([split]);
}
say qq{$_ }, join q{,} => @{$result{$_}} for sort keys %result;
__DATA__
A 1_x 9_z
A 1_x
A 1_x g_z
B 2_c
B 1_x 1_z
C 1_x 1_z
C v_x 8_z
D v_x s_x
E 1_x
F
This outputs:
$ pm_extract_1_x.pl
A 3,2
B 1,1
C 1,2
D 0,1
E 1,0
F 0,0
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.