in reply to Capturing Unique Data
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.
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.#!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
|
|---|