www.webdeveloper.com
Results 1 to 4 of 4

Thread: Stop duplicate results from FFDB in Perl

  1. #1
    Join Date
    Jun 2008
    Posts
    223

    Stop duplicate results from FFDB in Perl

    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.
    Code:
    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,
    Code:
    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.

  2. #2
    Join Date
    Oct 2007
    Location
    Vienna, Austria
    Posts
    392
    Code:
    my &#37;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?
    
    }

  3. #3
    Join Date
    Jun 2008
    Posts
    223

    Cool

    Hi Sixtease, right I see. The hash.

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

    Does the stuff.
    Thank you very much.

  4. #4
    Join Date
    Jun 2008
    Posts
    223
    Just did a test with a file of mock blog entries (size 2.5mb) and this code handled it with ease.

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

    Thanks again.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles