Nope, just couldn't leave it alone. Here's a solution to analyzing the Usenet hierarchies data. Note this is cumulative: repeated entries add together.

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 [^.]+ (?: [.] [^.]+){$dept +h} }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

Output:

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.*'

Update: The statement
    next LEVEL unless $category =~ m{ \A \Q$sub_cat\E }xms;
in the  LEVEL loop is totally useless: a sub-string extracted from beginning of a string must be present at the beginning of the string! Generated output is the same without this statement.


In reply to Re^4: I think regex Should Help Here... but How!? by AnomalousMonk
in thread I think regex Should Help Here... but How!? by ozboomer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.