I took a guess here since these 11-20 and 1-10 things look suspiciously like dates. Now maybe these are chapter numbers or something like that? I'm not sure.

One point is that the normal alpha-numeric sort works if you have leading zero'es. Otherwise, the sort order will not be numeric. So I just added a leading zero for the single digits, this is what you would need to sort chapters or dates easily.

Then I used a hash to count the number of occurences of each digit combo, sorted by that number combo and printed result.

#!usr/bin/perl -w use strict; my %date_hash; my @data = qw( NCLEGEND-11-20 NCLEGEND-11-20 NCLEGEND-11-2 NCLEGEND-1-10 NCLEGEND-1-10 NCLEGEND-1-10 NCLEGEND-1-20 NCLEGEND-1-20); foreach my $line (@data) { chomp ($line); #needed if @data is a file handle $line =~ s/-(\d)-/-0$1-/; #add leading zero for month $line =~ s/-(\d)$/-0$1/; #add leading zero for date my $date = ($line =~ m/NCLEGEND\-([\d-]+)/)[0]; #get the "num" part $date_hash{$date}++; } foreach my $date (sort keys %date_hash) { print "$date $date_hash{$date}\n"; #just print "$date\n"; if no need for counter value } __END__ Prints: 01-10 3 01-20 2 11-02 1 11-20 2
Update: This is such an important part of being easily to sort reports by date, that some "amplification" is justified: "2009-08-05" is FAR superior to just "2009-8-5", because the natural alpha sort order will do the right thing for the longer string with leading zero'es. For times, same thing goes: 01:25 is FAR better than 01:25 AM and if you mean 01:25 PM, use 24 hour time 13:25. "2009-08-05 13:25" can be sorted against "2008-08-15 01:25" with just the basic sort in Perl.

In reply to Re: Capturing Unique Data by Marshall
in thread Capturing Unique Data by donkost

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.