CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
I need a code for duplicate checker in perl


New User

Jul 25, 2011, 7:29 AM

Post #1 of 2 (9599 views)
I need a code for duplicate checker in perl Can't Post

Hi everyone..I am a perl newbie..I have a tab delimited text file with two columns. Now I am trying to make a code that will append the text file after taking user input. But it should check the whole for duplicates. If duplicates found, it doesn't append. if there's no duplicate, then it appends. Please note that it only check the first column for duplicate. Here's the code I have written. Currently, it only checks the first row. And if duplicate is found, it shows a message like "Found match" and also appends the duplicates. I have attached my text file also.
The code is:

print "Please write your name and address below\n"; 

chomp $input;
chomp $input1;

open FILE, "C:/Software/new1.txt" or die $!;

while ($line=<FILE>) {
@line=split("\t", $line);
if ($input=~ m/$name/)
print "Name matching\n";
open FILE, ">>C:/Software/new1.txt" or die $!;
print FILE "$input\t$input1";

close (FILE);

Please note that, it also doesn't match the user input strictly. Like if the first column of first row is abc and the user input was abcd..still it takes the value..
Please help
Attachments: new1.txt (0.16 KB)


Aug 10, 2011, 7:34 PM

Post #2 of 2 (8465 views)
Re: [pratul] I need a code for duplicate checker in perl [In reply to] Can't Post

Your attached text file has several duplicates of 'dey'. Does this mean that it should never be appended to? My version 0.0 assumes that it does. Are 'names' different if they differ only in case or surrounding whitespace. Version 0.0 assumes that they are different. If you are not using ActiveState perl, you will have to replace the prompt module with one that works on your system.

use strict; 
use warnings;
use ActiveState::Prompt qw( prompt );
my $user_name = prompt( 'Enter Your Name: ' );
my $user_addr = prompt( 'Enter Your Address: ' );
open my $NEW1, '<', 'new1.txt' or die $!;
my %old_data;
while (my $line = <$NEW1>) {
my( $old_name, $old_addr ) = split /\t/, $line;
if (exists $old_data{$old_name}) {
die "Duplicate entries '$old_name' already in file\n";
$old_data{$old_name} = $old_addr;
close $NEW1;
if (exists $old_data{$user_name}) {
die "Name '$user_name' already exists in file\n";
open $NEW1, '>>', 'new1.txt' or die $1;
print {$NEW1} "$user_name\t$user_addr\n";
close $NEW1;

Good Luck,


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives