how to avoid displaying duplicates

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pgrayove
    New Member
    • Oct 2006
    • 1

    how to avoid displaying duplicates

    I'm a first time poster and I would really appreciate any help. I'm working with a MySQL database using a Perl script to access the data and display as a web page. I'm stuck with one part. The database has repeated events in it. These events have the same title, but different dates/times. I would like to select all of the dates and times for each event, but only print each title one time. Right now I'm getting something like this:

    Alef Ba--Arabic for preschoolers Sat, Oct 21 - 10:00 AM
    Alef Ba--Arabic for preschoolers Sat, Oct 28 - 10:00 AM
    Say it in Spanish Thu, Oct 19 - 9:30 AM
    Say it in Spanish Thu, Oct 26 - 9:30 AM
    Say it in Spanish Thu, Nov 2 - 9:30 AM
    Your Child, Ready to Read Tue, Oct 17 - 10:00 AM
    Your Child, Ready to Read Tue, Oct 24 - 10:00 AM

    What I want is something like this:
    Alef Ba--Arabic for preschoolers Sat, Oct 21 - 10:00 AM
    Sat, Oct 28 - 10:00 AM
    Say it in Spanish Thu, Oct 19 - 9:30 AM
    Thu, Oct 26 - 9:30 AM
    Thu, Nov 2 - 9:30 AM
    Your Child, Ready to Read Tue, Oct 17 - 10:00 AM
    Tue, Oct 24 - 10:00 AM


    Thanks,
    Paula
  • miller
    Recognized Expert Top Contributor
    • Oct 2006
    • 1086

    #2
    I'm going to run with two assumptions.
    Assumption 1) Part of your problem is the fact that Date is included with the Title in the database
    Assumption 2) You're already pulling the data sorted by Title.

    Assuming this, you have only one problem, namely separating the title from the date. This can be accomplished with a simple regular expression. I'm going to also be assuming that your dates are of the form that you stated above, otherwise you might end up with problems.

    Code:
    #!/usr/bin/perl
    
    my @data = (
    	'Alef Ba--Arabic for preschoolers Sat, Oct 21 - 10:00 AM',
    	'Alef Ba--Arabic for preschoolers Sat, Oct 28 - 10:00 AM',
    	'Say it in Spanish Thu, Oct 19 - 9:30 AM',
    	'Say it in Spanish Thu, Oct 26 - 9:30 AM',
    	'Say it in Spanish Thu, Nov 2 - 9:30 AM',
    	'Your Child, Ready to Read Tue, Oct 17 - 10:00 AM',
    	'Your Child, Ready to Read Tue, Oct 24 - 10:00 AM',
    );
    
    my $lastTitle = '';
    foreach my $record (@data) {
    	$record =~ s{^(.*) (\w*,.*)$}{$1};
    	my $date = $2;
    	
    	if ($lastTitle ne $record) {
    		$lastTitle = $record;
    		print "$record $date\n";
    	} else {
    		print "$date\n";
    	}
    }
    
    1;
    
    __END__
    Please note that another good way to test for duplicates is to use a hash. Because I was assuming the data was already in order though, it more efficient memory wise to use a scalar to simply keep track of the last title seen instead of all the titles seen.

    Comment

    Working...