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!

Adding/getting info from files (pretty basic I guess)

Status
Not open for further replies.

c4n

Programmer
Mar 12, 2002
110
SI
How can I do this in CGI:
Here's what I wanna know:

- I have a file named "file.txt"
- In this file I have many words, seperated by '%%'
(%%wrod1%%word2%%word3 etc ...)

- Now somebody wants to add a word. What code should I put to make the script first seperate those words, then check if the word somebody is trying to submit is already listed. If YES it should write it in "error.txt" if NOT add it to the list.

I tried something with 'foreach' function, but can't seem to get it working.


Thanks in advance!
 
Try the following:

$list = "%%wrod1%%word2%%word3";

%words = map {$_ => 1} split("%%",$list);
if($words{$new_word}) {
open ERROR, ">>error.txt" or die "Cannot open error file\n";
print ERROR "$new_word\n";
close ERROR;
} else {
$list .= "%%$new_word";
}


Additionally you should lock the error file while writing to prevent conflicts if more than one person runs the script at the same time. See flock() for locking files.

HTH,
Barbie.
Leader of Birmingham Perl Mongers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top