Click to See Complete Forum and Search --> : Stop duplicate results from FFDB in Perl


edatz
01-14-2012, 04:15 AM
Hi, I'm picking the field the way I want from a flatfile database in Perl.

However I do not know how to get rid of the duplicates.

use strict;
use warnings;
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
print "Content-type: text/html\n\n";

foreach my $rec (<DATA>) {
my @fd=split /\|/, "$rec";
my $dt=$fd[3];
my @tmm = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
my $tyr = substr $dt,0,+4; # year (nums 1-4)
my $tmo = substr $dt,4,+2; # month (nums 5&6)
$tmo=$tmo-1; # compensate for 0 in month
my $dts = "$tmm[$tmo] $tyr";

print qq ~<a href="#">$dts</a><br>~; # How do I stop the duplicates?

}
exit(0);

=holdResults
This is how it shows with the duplicates
Jan 2012
Jan 2012
Dec 2011
Dec 2011
Nov 2010

It should show
Jan 2012
Dec 2011
Nov 2010
=cut

__DATA__
5|EEE|eeeeee|201201|
4|DDD|dddddd|201201|
3|CCC|cccccc|201112|
2|BBB|bbbbbb|201112|
1|AAA|aaaaaa|201011|


We did try something about repeats a year or so back,
my $dts{$_}++; next if $dts{$_} > 1;
but I think there's an error in it (not sure how to use it anyways).

Hope someone can help, Many Thanks.

Sixtease
01-15-2012, 01:55 AM
my %occurred;
foreach my $rec (<DATA>) {
my @fd=split /\|/, "$rec";
my $dt=$fd[3];
my @tmm = ('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
my $tyr = substr $dt,0,+4; # year (nums 1-4)
my $tmo = substr $dt,4,+2; # month (nums 5&6)
$tmo=$tmo-1; # compensate for 0 in month
my $dts = "$tmm[$tmo] $tyr";

next if $occurred{$dts};
$occurred{$dts} = 1;

print qq ~<a href="#">$dts</a><br>~; # How do I stop the duplicates?

}

edatz
01-15-2012, 03:35 AM
Hi Sixtease, right I see. The hash.

After I posted this I wondered about hashes but got nowhere, the line
$occurred{$dts} = 1;
is something I didn't realize.

Does the stuff.:cool:
Thank you very much.

edatz
01-16-2012, 09:32 AM
Just did a test with a file of mock blog entries (size 2.5mb) and this code handled it with ease.:D

It's used for my blog archive drop down and should more than suffice for the next few years.

Thanks again.