in reply to List manipulation

See the comments for explanation:
#!/usr/bin/perl use strict; use warnings; use feature 'say'; my $last = ''; # Remember the id. my $count_1_x; my $count_nonblank; while (<DATA>) { my ($id, $col2, $col3) = split; if ($id ne $last) { # A new record begin +s. say "$last $count_1_x $count_nonblank" if $last; # Do not print at th +e very beginning. $last = $id; $count_1_x = $count_nonblank = 0; } $count_1_x++ if '1_x' eq $col2; $count_nonblank++ if defined $col3 and length $col3; } say "$last $count_1_x $count_nonblank" if $last; # Print for the last + record. __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
Update: Updated to comfort warnings on emtpy column 3.

Replies are listed 'Best First'.
Re^2: List manipulation
by tobyink (Canon) on May 31, 2012 at 17:59 UTC

    This was more an experiment in whether it would be possible, than a piece of sound advice...

    use 5.010; use List::MoreUtils qw< part >; say for map { my $x = $_; sprintf( '%s %d,%d', $x->[0][0], scalar(grep { $_->[1] eq '1_x' } @$x), scalar(grep { defined $_->[2] } @$x), ) } part { # me thinks List::MoreUtils needs a way to make this simpler state $part = 0; state $last = undef; $part++ if defined $last && $last ne $_->[0]; $last = $_->[0]; $part } map { chomp; [split /\s+/] } sort <DATA>; __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
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'