=================
Code 1:
=================
open(FILE, "$data");
@emails = <FILE>;
close(FILE);
if($#emails >= 10) { &maxerror; }
else {
open(FILE, ">>$data");
print FILE "$popboxname,$popboxpasswd\n";
close(FILE);
print "Location: $url\n\n";
}
=================
Code 2:
=================
foreach (@emails) {
&existerror if /$popboxname,/i;
}
I would really appreciated anyones suggestions.
Cheers
Linda
would really appreciated anyones suggestions
I hope you don't mind if I answer in the spirit of the Forum Charter. ( [webmasterworld.com...] ) i.e. "...We want to teach people to help themselves. (Give a man a fish, and you feed him for a day. Teach a man to fish, you feed him for a life time..."
I suggest you take your English description of the problem, and refine it until it becomes clearer. Once you do that, the code will become obvious.
For example, you wrote:
the first thing is to check if there is 10 lines, if there is then it should check if there is anything that contains $popboxname, if there is then print a error message if not then proceed with writing data.
You probably meant:
the first thing is to check if there is less than 10 lines, if there is then it should check if there is anything that contains $popboxname, if there is then print a error message if not then proceed with writing data.
Now take a higlighter pen and highlight the words "if", "then", "else"; perhaps reword some things (e.g. instead of "if there is then print a error message if not then proceed with writing data", how about: "if there is then print a error message else proceed with writing data"
You can do it. I know you can. Post back showing what you tried and if it doesn't work, what did happen...
Shawn
open(FILE, "$data");
@emails = <FILE>;
close(FILE);
foreach (@emails) {
&existerror if /$popboxname,/i;
}
if($#emails >= 10) { &maxerror; }
else {
open(FILE, ">>$data");
print FILE "$popboxname,$popboxpasswd\n";
close(FILE);
print "Location: $url\n\n";
}
Here is what I meant in my previous post:
Say you start with
the first thing is to check if there is less than 10 lines, if there is then it should check if there is anything that contains $popboxname, if there is then print a error message if not then proceed with writing data.
reword that a bit:
check if there is less than 10 lines. If there is then check if there is anything that contains $popboxname, if there is then print a error message, else proceed with writing data.
reword that a bit more:
If there is less than 10 lines then if there is anything that contains $popboxname then print a error message, else proceed with writing data.
Take out your highlighter pen:
If there is less than 10 lines
then
____if there is anything that contains $popboxname
____then print a error message,
____else proceed with writing data.
Refine some more:
If there are 10 lines or more
then print error message
else
____if there is anything that contains $popboxname
____then print a error message,
____else proceed with writing data.
You can do it...
syntax error at test.cgi, near "else"
here is the bottom part of the code:
if($#emails >= 10) { &maxerror; }
foreach (@emails) {
&existerror if /$popboxname,/i;
}
}
else {
open(FILE, ">>$data");
print FILE "$popboxname,$popboxpasswd\n";
close(FILE);
print "Location: $url\n\n";
}
First you open $data and read all lines into the array @emails (each line, delimited by \n, is a separate element). Then you check to see if there are >= 10 elements in the array (i.e. 11 or more lines in the data file; array elements start at 0). If this test passes you open $data to append (>>) and write $popboxname,popboxpasswd
So, where do $popboxname and $popboxpasswd come from. I don't see either of those variable set in the code.
Scott Geiger
The problem is not in your use of @... It is in your understanding of the syntax of the if (TEST) {BLOCK} else {BLOCK}. Either way, I suggest you work your way through a basic perl tutorial, either on line or with a book. Generally that stuff is covered in the first 3 chapters of most basic perl books, so it should be relatively painless.
The reason you are getting a syntax error is that you have an 'else' but there is no 'if' before it, and you have an extra '}' which shouldn't be there. Once you fix those syntax errors, it will still not do what you want because your code doesn't match the logic that you are trying to achieve (It doesn't match the English description you wrote in msg #1).
Look at what I had in my previous post:
If there are 10 lines or more
then print error message
else
____if there is anything that contains $popboxname
____then print a error message,
____else proceed with writing data.
Massage it a bit more and you should arrive at something like:
if ($#emails >= 10)
{
____&maxerror;
} elsif (@check_for_popboxname)
{
____&existerror;
} else {
____open(FILE, ">>$data");
____print FILE "$popboxname,$popboxpasswd\n";
____close(FILE);
____print "Location: $url\n\n";
}
and define a function check_for_popboxname which does something like:
{
____foreach (@emails) {
________return "true" if /$popboxname,/i;
____}
____return ""; # false
}
This is not great style, but just an illustration to show you how to get from your English explanation of what you want to do, to code which follows the logic you want.
Shawn
[perldoc.com...]
The term 'function' and 'subroutine' are pretty much synonymous. Perl calls the ones you define subroutines, and the in-built ones functions.
Sorry for causing confusion. I guess for that I owe it to you to stop being a smart-arse and just show you how:
sub check_for_popboxname
{
foreach (@emails) {
return "true" if /$popboxname,/i;
}
return ""; # false
}
But still, I REALLY REALLY suggest you reserve some time for yourself to work through a turotial or book. I'm sure you are busy, and it is hard to find the time, but once you do that you will be much more productive. In addition to the previous url, here is another good source:
[learn.perl.org...]
I think I speak for most at this forum when I say this: I'm happy to answer really basic questions (hey, I have a lot of them myself). But I get much more satisfaction when I see that I am helping someone improve their knowledge and skills, rather than just providing a script or snippet they copy in. (Hope I worded that correctly and it doesn't come across unfriendly; it isn't meant to be)
Shawn