use warnings FATAL => 'all' ; use strict; use Text::CSV_XS; use Data::Dump; use List::Util qw(max); MAIN: { # main execution loop my $csv = Text::CSV_XS->new ({ binary => 1 }) or die qq{instantiating CSV: }, Text::CSV_XS->error_diag(); my %postings; RECORD: # for my $ar_record (@{ $csv->getline_all(*DATA) }) { # works while (my $ar_record = $csv->getline(*DATA)) { # works my ($category, $posted) = @$ar_record; LEVEL: for (my $depth = 0; my ($sub_cat) = $category =~ m{ \A [^.]+ (?: [.] [^.]+){$depth} }xmsg; ++$depth ) { # print qq{depth $depth: '$sub_cat' \n}; # FOR DEBUG next LEVEL unless $category =~ m{ \A \Q$sub_cat\E }xms; $postings{$sub_cat}{posted} += $posted; $postings{$sub_cat}{lowest} = $sub_cat eq $category; } # end for LEVEL } # end for RECORD # dd \%postings; # FOR DEBUG my $max_width = max map { length $_->{posted} } values %postings; for my $sub_category (reverse sort keys %postings ) { printf qq{%*d for '$sub_category%s' \n}, $max_width, $postings{$sub_category}{posted}, $postings{$sub_category}{lowest} ? '' : '.*' ; } exit(0); # expected exit } # end MAIN loop exit(1); # unexpected MAIN loop exit # subroutines ###################################################### # none __DATA__ comp.lang.c,100 comp.lang.beta,23 comp.lang.java.help,123 comp.object,12 alt.3d,12 comp.object,3 comp.lang.beta,11 alt.animals.llama,1423 #### 15 for 'comp.object' 123 for 'comp.lang.java.help' 123 for 'comp.lang.java.*' 100 for 'comp.lang.c' 34 for 'comp.lang.beta' 257 for 'comp.lang.*' 272 for 'comp.*' 1423 for 'alt.animals.llama' 1423 for 'alt.animals.*' 12 for 'alt.3d' 1435 for 'alt.*'