Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking for Duplicates in a File

Status
Not open for further replies.

rhnewfie

Programmer
Jun 14, 2001
267
CA
Hopefully someone can help. I have a simple CGI script that takes form data from a webpage and writes it to a text file. I need to alter the script so that it the script will check the file and see if the entry I am about to write already exists and then write it if it does not exist or exit if it does. Has anyone done this before that could help? Here is the script as it stands now.

#!/usr/bin/perl

$file ="/usr/domains/output/Subscribe.txt";

if ($ENV{'REQUEST_METHOD'} eq 'POST')
{
read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
@pairs = split(/&/, $buffer);
foreach $pair (@pairs)
{
($name, $value) = split(/=/, $pair);
$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$contents{$name} = $value;
}
}

if (-f $file)
{
#umask 002;
open(OUTPUT, ">>$file");
#chmod 0666, $file;
print "Content-type: text/plain\n\n ";
print OUTPUT RPad("$contents{'field'}",30);
print OUTPUT RPad("$contents{'field'}",30);
print OUTPUT RPad("$contents{'field'}",30);
print OUTPUT RPad("$contents{'field'}",5);
print OUTPUT RPad("$contents{'field'}",30);
print OUTPUT RPad("$contents{'field'}",10);
print OUTPUT RPad("$contents{'field'}",30);
print OUTPUT RPad("$contents{'field'}",30);
print OUTPUT RPad("$contents{'field'}",4);
print OUTPUT RPad("$contents{'field'}",5);
print OUTPUT RPad("$contents{'field'}",3);
print OUTPUT RPad("$contents{'field'}",3);
print OUTPUT RPad("$contents{'field'}",4);
print OUTPUT RPad("$contents{'field'}",3);
print OUTPUT "$contents{'field'}\n";
close (OUTPUT);
}
else
{
umask 002;
open(OUTPUT, ">>$file");
close (OUTPUT);
chmod 0666, $file;
#chown 318, , $file;
open(OUTPUT, ">>$file");
flock OUTPUT, 2;
print "Content-type: text/plain\n\n ";
print OUTPUT RPad("$contents{'field'}",30);
print OUTPUT RPad("$contents{'field'}",30);
print OUTPUT RPad("$contents{'field'}",30);
print OUTPUT RPad("$contents{'field'}",5);
print OUTPUT RPad("$contents{'field'}",30);
print OUTPUT RPad("$contents{'field'}",10);
print OUTPUT RPad("$contents{'field'}",30);
print OUTPUT RPad("$contents{'field'}",30);
print OUTPUT RPad("$contents{'field'}",4);
print OUTPUT RPad("$contents{'field'}",5);
print OUTPUT RPad("$contents{'field'}",3);
print OUTPUT RPad("$contents{'field'}",3);
print OUTPUT RPad("$contents{'field'}",4);
print OUTPUT RPad("$contents{'field'}",3);
print OUTPUT "$contents{'field'}\n";
flock OUTPUT, 8;
close (OUTPUT);
}


sub RPad {

local($str, $len, $chr) = @_;

$chr = " " unless (defined($chr));

return substr($str . ($chr x $len), 0, $len);

}

exit;

Any help would really be appreciated.

Thanks
RHNewfie There are 3 Types of People in the World
Those Born to Think Logically
Those that can Learn to Think Logically
Those that Shouldn't Try
 
This script also seems to create (or something creates) a tonne of spaces in the text file!! It starts with one and then more and more... then goes back to one and starts again.

Any ideas?? There are 3 Types of People in the World
Those Born to Think Logically
Those that can Learn to Think Logically
Those that Shouldn't Try
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top