in reply to grep the data out of a text file.

Store the counts in a hash keyed by the category (i.e. A, B in your example). You also need a variable to remember the current category, its scope must be wider than the filehandle reading loop, because the number comes on a different line than the category, i.e. in a different iteration of the loop.

#!/usr/bin/perl use warnings; use strict; my %violations_by_category; my $category; while (<DATA>) { if (/^([A-Z])_[0-9]+:/) { $category = $1; } elsif (/([0-9]+) violations?/) { $violations_by_category{$category} += $1; } } for my $category (keys %violations_by_category) { print $category, " ", $violations_by_category{$category}, "\n"; } __DATA__ A_01: xxxxxxx xxxxxxxxxx xxx......... 1 violation A_02: xxxxxxx xxxxxxxxxx xxx......... 4 violations B_02: xxxxxxxx xxxxxxxxxx xxx......... 3 violations

($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,