print("\n\nOutlook CSV file duplicate search and deletion\n"); open (CONTACTS, "contacts.csv") or goto OPENFAIL; $first = 1; $Display = 0; $EntryCnt = 0; $Part = 1; while () { if ( $first ) { @Headings = split /,/; $first = 0; } else { if ($Part == 1) { $EntryCnt++; $Quote = 0; $Comma = 0; $Entry = $_; @Parse = split /,/; push @Names, $Parse[1].$Parse[2].$Parse[3]; if ( $Display++ > 10) { $Display = 0; print "\rReading: ".$#Names; } } else { $Entry = $Entry.$_; } $Part++; $_ = reverse $_; while ($_) { $Char = chop $_; if ($Comma) { if ($Char eq '"') { $Comma = 0; $Quote = 1; } } elsif ($Quote) { if ($Char eq '"') { $Quote = 0; } } elsif ($Char eq ',') { $Comma = 1; } elsif ($Char eq '"') { $Quote = 1; } } if (($Comma == 0) && ($Quote == 0)) { push @Contacts, $Entry; $Part = 1; } } } print "\rReading: ".$#Names."\n"; $Index = 0; $Display = 0; while ($Index < $EntryCnt) { $LookAhead = 1; while ($LookAhead <= 5) { if ( $Display++ > 10) { $Display = 0; print("\rDuplicate search: ".$Index); } if ($Names[$Index] eq $Names[$LookAhead+$Index]) { if ((length $Contacts[$Index]) ge (length $Contacts[$LookAhead+$Index])) { splice @Names, $LookAhead+$Index, 1; splice @Contacts, $LookAhead+$Index, 1; } else { splice @Names, $Index, 1; splice @Contacts, $Index, 1; $LookAhead = 1; } } else { $LookAhead++; } if ($Index >= $#Names) { goto FINISH; } } $Index++; } FINISH: print("\rDuplicate search: ".$Index); print("\nCompleted: result in contacts_.csv\n\n"); close CONTACTS; open (CONTACTS_, ">contacts_.csv") or goto CREATEFAIL; printf CONTACTS_ (join ',',@Headings); foreach $_ (@Contacts) { printf CONTACTS_ $_; } close CONTACTS_; goto EXIT; OPENFAIL: print"Could not open contacts.csv\n"; goto EXIT; CREATEFAIL: print "Could not create contact_.csv\n"; goto EXIT; EXIT: printf("Press ENTER to exit"); $key = ; # if ($PrevSplit) # { # do # { # $Entries[$#Entries] = $Entries[$#Entries].(shift @Parse); # } while (($#Parse >= 0) && (($Entries[$#Entries] =~ s/\"/\"/g) != 2)); # if (($Entries[$#Entries] =~ s/\"/\"/g) == 2) # { # $PrevSplit = 0; # } # } # push @Entries, @Parse; # if ($#Parse >= 0) # { # $FoundCnt = ($Parse[$#Parse] =~ s/\"/\"/g); # if ($FoundCnt == 2) # { # push @Names, $Entries[1].$Entries[2].$Entries[3]; # push @Contacts, (join ',', @Entries); # undef @Entries; # $EntryCnt++; # } # elsif ($FoundCnt == 1) # { # $PrevSplit = 1; # } # } # } #}