Forum Moderators: coopster & phranque

Message Too Old, No Replies

Unable to combine these two pieces of perl code

         

lindajames

1:13 pm on May 14, 2003 (gmt 0)

10+ Year Member



Hi, I have the following code, that checks if a text file contains 10 lines, if so it displays &maxerror; otherwise it writes the value of $popboxname and $popboxpasswd in a the text file seperated by commas. I also have a code, that ive listed below this one. the code checks whether there is any lines in the text file containing $popboxname, if so it displays and error. The problem i am having is combining the two together, i want to combine the second code into the first so that the first code checks two things before writing data to the text file, 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.

=================
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

ShawnR

1:47 pm on May 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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

lindajames

2:14 pm on May 14, 2003 (gmt 0)

10+ Year Member



Ive tried all different ways, but this seems to be the closest i can get, But with this way it displays the error, but still seems to write the data to the text file, obviously its something ive done wrong, but i cant see anything wrong with it.

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";
}

ShawnR

2:33 pm on May 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



One good way to check code is called a 'walk-through'. Just pretend you are the computer, and go through the code line by line and you will see why the data is being written even though that mailbox name is already in the file.

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...

lindajames

2:49 pm on May 14, 2003 (gmt 0)

10+ Year Member



ive done it so many ways but i still dont understand the how to use these @ ~ etc in querying text files, ive tried the following, but this time i get the following error:

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";
}

BCMG_Scott

6:59 pm on May 14, 2003 (gmt 0)

10+ Year Member



Ok I may be missing something here.

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

lindajames

7:50 pm on May 14, 2003 (gmt 0)

10+ Year Member



$popboxname and $popboxpasswd are actually parameters passed by the previous form. Like this:

my $popboxname = $query->param('popboxname');
my $popboxpasswd = $query->param('popboxpasswd');

Hope someone can help me solve this problem

Cheers
Linda

ShawnR

11:13 pm on May 14, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Linda

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

lindajames

12:26 pm on May 15, 2003 (gmt 0)

10+ Year Member



i tried to define the function check_for_popboxname like this but im getting syntax errors relating to the check_for_popboxname function:

@check_for_popboxname
{
foreach (@emails) {
return "true" if /$popboxname,/i;
}
return ""; # false
}

ShawnR

12:45 pm on May 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There are a few examples of how to define subroutines here:

[perldoc.com...]

lindajames

1:00 pm on May 15, 2003 (gmt 0)

10+ Year Member



but you said i need to define a function for check_for_popboxname, i know how to create sub-routines, is that what you mean? in the example you called } elsif (@check_for_popboxname) @check_for_popboxname doesnt seem like a sub-routine to me, sub-routines are called like &check_for_popboxname; aint that right? im very confused

ShawnR

1:17 pm on May 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OOPS, my mistake, I meant &check_for_popboxname not @check_for_popboxname

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

lindajames

1:36 pm on May 15, 2003 (gmt 0)

10+ Year Member



Yes, i am actually reading alot of tutorials at the moment and aim to self teach myself, but this issue was quiet important. Anyway, thanx for your help, it seems to be doing the trick now.

Cheers
Linda

ShawnR

1:52 pm on May 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well done for getting it going!