D
\Dandy\ Randy
Hi, I'm trying to replace the ID number in my @array. Each entry of
addressbook.txt has an ID number, a name and an email address separated by a
"|" in this format:
1|Robert|[email protected]
2|Gerry|[email protected]
3|Frank|[email protected]
A user simply inputs a number in a web form, then the data is passed to this
deletion script:
# Get array entries
open (ADDRESSES, "<data/addressbook.txt") or die "Can't open file: $!";
@recipients=<ADDRESSES>;
close(ADDRESSES);
# Loop to locate and delete entry based on form data
foreach $recipients(@recipients) {
($id,$name,$email)=split(/\|/,$recipients);
$count++;
if ($id eq "$form{'id'}") {
$count--;
splice(@recipients, $count, 1);
}
}
########
# Missing code here to adjust ID numbers
########
# Print the changed bata back to file
open (ADDRESSES, ">data/addressbook.txt") or die "Can't open file: $!";
print ADDRESSES @recipients;
close(ADDRESSES);
So far this script works correctly. Lets say I entered "2" into the web
form, after the script is run, addressbook.txt would now contain the
following:
1|Robert|[email protected]
3|Frank|[email protected]
Notice the ID numbers are 1 & 3 ... I would like to add another peice of
code to the above script that will change the ID number based on entries
remaining in the text file ... when the @array data gets saved back to the
text file, it would show as:
1|Robert|[email protected]
2|Frank|[email protected]
I tried this "replacement" method in the "Missing code" section but didn't
work:
foreach $recipients(@recipients) {
($id,$name,$email)=split(/\|/,$recipients);
$newid++;
$recipients =~ s/$id/$newid/g;
}
Can someone help point me in the right direction to accomplish my goal? COde
examples welcomed. Thanx everyone.
Randy
addressbook.txt has an ID number, a name and an email address separated by a
"|" in this format:
1|Robert|[email protected]
2|Gerry|[email protected]
3|Frank|[email protected]
A user simply inputs a number in a web form, then the data is passed to this
deletion script:
# Get array entries
open (ADDRESSES, "<data/addressbook.txt") or die "Can't open file: $!";
@recipients=<ADDRESSES>;
close(ADDRESSES);
# Loop to locate and delete entry based on form data
foreach $recipients(@recipients) {
($id,$name,$email)=split(/\|/,$recipients);
$count++;
if ($id eq "$form{'id'}") {
$count--;
splice(@recipients, $count, 1);
}
}
########
# Missing code here to adjust ID numbers
########
# Print the changed bata back to file
open (ADDRESSES, ">data/addressbook.txt") or die "Can't open file: $!";
print ADDRESSES @recipients;
close(ADDRESSES);
So far this script works correctly. Lets say I entered "2" into the web
form, after the script is run, addressbook.txt would now contain the
following:
1|Robert|[email protected]
3|Frank|[email protected]
Notice the ID numbers are 1 & 3 ... I would like to add another peice of
code to the above script that will change the ID number based on entries
remaining in the text file ... when the @array data gets saved back to the
text file, it would show as:
1|Robert|[email protected]
2|Frank|[email protected]
I tried this "replacement" method in the "Missing code" section but didn't
work:
foreach $recipients(@recipients) {
($id,$name,$email)=split(/\|/,$recipients);
$newid++;
$recipients =~ s/$id/$newid/g;
}
Can someone help point me in the right direction to accomplish my goal? COde
examples welcomed. Thanx everyone.
Randy