[prog] Introduction and Perl: Flat File DB Query Question

Jacinta Richardson jarich at perltraining.com.au
Wed Mar 15 10:14:41 EST 2006


Sabine Konhaeuser wrote:

> for ($j = 0; $j <= $centers_found; $j ++) {
>     open (INPUTFILE, "$rc_data") || die print STDOUT "Can't open $rc_data\n";
>     while (<INPUTFILE>) { #begin while
>         chop;
>         ($rc_name, $street_address, $city, $state, $rc_zip, $email, $phone1, 
> $phone2, $fax) = split (/\|/);
> # the above 2 lines are on one line
>         if ($rc_zip eq $zip[$j]) { 
>           # show the dealers that match the zip/distance
>           # prints out the locations
>         } # end if
>     } # end while
>     close (INPUTFILE);
> 
> } # end for

The above snippet is where you need to edit to remove duplicates.  Perhaps you
could do something like:

my %centers;
> for ($j = 0; $j <= $centers_found; $j ++) {
>     open (INPUTFILE, "$rc_data") || die print STDOUT "Can't open $rc_data\n";
>     while (<INPUTFILE>) { #begin while
>         chop;
>         ($rc_name, $street_address, $city, $state, $rc_zip, $email, $phone1,
> $phone2, $fax) = split (/\|/);
> # the above 2 lines are on one line
>         if ($rc_zip eq $zip[$j]) {

               next if $centers{$rc_name,$street_address,$city}++;

>              # print out the locations
>         }
>     }
>     close (INPUTFILE);
> }

This assumes two things.  That $rc_name,$street_address,$city is unique per
business, and that you're getting this error because in some situations $zip[x]
and $zip[x+n] might be the same.

If you have the same business appearing twice in the file, but with ever so
slightly different values for $rc_name or the other fields... perhaps an extra
space or something, this won't help you.

There are other places you can fix this.  You could check to see that all the
numbers in $zip are unique.  But this should prevent the situation where the
exact same record is displayed twice.

All the best,

    Jacinta

-- 
   ("`-''-/").___..--''"`-._          |  Jacinta Richardson         |
    `6_ 6  )   `-.  (     ).`-.__.`)  |  Perl Training Australia    |
    (_Y_.)'  ._   )  `._ `. ``-..-'   |      +61 3 9354 6001        |
  _..`--'_..-_/  /--'_.' ,'           | contact at perltraining.com.au |
 (il),-''  (li),'  ((!.-'             |   www.perltraining.com.au   |




More information about the Programming mailing list