...

Perl Function Problem

premshree
09-21-2002, 08:00 AM
Hello,

I am using the following code to test for operators :

sub isOperator
{
($who)=@_;
if($who=="+" || $who=="-"|| $who=="*" || $who=="/" || $who=="^")
{
return(true);
}
else
{
return(false);
}
}

$get=<STDIN>;
if(isOperator($get))
{
print $get;
}


The code should return true only when I enter '+', '-', '*', '/' or '^' but the code returns true for anything that I enter.

I can't figure out what is wrong. Please help!

FlashCoder
09-21-2002, 09:03 AM
Try instead "==" will write "eq".
Ok?

premshree
09-21-2002, 06:18 PM
You mean instead of using '==' to use 'eq' ?

Mouldy_Goat
09-21-2002, 07:51 PM
Yes, use 'eq' instead of '=='. The former does a string comparison, whilst the second does a numerical comparison, which isn't what you want here.

Two other points worth noting:

Firstly, in your function you're writing return (true);. What this will actually do is return the string 'true', and so whilst it will evaluate to true in a boolean context - it means that return (false); will also return true.

Perl doesn't have any boolean 'true' or 'false'. Instead just use a '1' and a '0' to get the same effect - it's less typing anyway.

Also you're forgetting to chomp() $get. This means getting rid of the newline (if any) on the end of the variable, which will cause all of your equality tests in the isOperator() function to return false, because "+\n" is not the same as "+". (The \n stands for newline).

So your script should probably look something like this:
sub isOperator
{
($who) = @_;
if($who eq "+" || $who eq "-" || $who eq "*" || $who eq "/" || $who eq "^")
{
return 1;
}
else
{
# because nothing is being returned, this will evaluate to false
return;
}
}

$get = <STDIN>;
chomp $get;
if(isOperator($get))
{
print $get;
}

Hope that helps a bit.

premshree
09-22-2002, 03:33 PM
Yea that worked! Thanks.

FlashCoder
09-22-2002, 08:08 PM
Originally posted by premshree
Yea that worked! Thanks.

:)

premshree
09-23-2002, 09:50 AM
I have a file print.pl


print '<html><head><title>Welcome</title></head><body>';
print param('exp');
print '</body></html>';


I run the file in the browser with URL as
http://localhost/cgi-bin/print.pl?exp=Premshree but the parameter 'exp' is not printed.

FlashCoder
09-23-2002, 05:47 PM
Originally posted by premshree
I have a file print.pl


print '<html><head><title>Welcome</title></head><body>';
print param('exp');
print '</body></html>';


I run the file in the browser with URL as
http://localhost/cgi-bin/print.pl?exp=Premshree but the parameter 'exp' is not printed.

code:

print $ENV{CONTENT_LENGTH};

Please, for more information e-mail me (bdenis@list.ru) and I ask to your question!!!
:)

premshree
09-24-2002, 03:05 PM
I am just trying to pass a parameter to a Perl file and print it on the browser. I don't want to use any CGI modules.

FlashCoder
09-24-2002, 06:30 PM
Originally posted by premshree
I am just trying to pass a parameter to a Perl file and print it on the browser. I don't want to use any CGI modules.

Sorry:

print $ENV{QUERY_STRING};

It write your parametr. It worked?

FlashCoder
09-24-2002, 06:32 PM
Originally posted by FlashCoder


Sorry:

print $ENV{QUERY_STRING};

It write your parametr. It worked?

For more information, Please e-mail me the letter in address^
den86@hotbox.ru or bdenis@list.ru and a will ask on your all questions and problems !!!

:) :) :)

premshree
09-25-2002, 08:02 AM
Yes, that printed the query string that I passed. Maybe I'll try to manipulate the query string to get the name-value pair.

FlashCoder
09-25-2002, 10:52 AM
Originally posted by premshree
Yes, that printed the query string that I passed. Maybe I'll try to manipulate the query string to get the name-value pair.

code:
($a, $b)=split(/=/,$ENV{QUERY_STRING});

print "\$a is ";$a;

Did I help you?

premshree
09-25-2002, 02:21 PM
I used this :


@string=split(/&/,$ENV{QUERY_STRING});
@exp=split(/=/,$string[0]);
$expression=$exp[1];
$expression =~ tr/+/ /;
$expression =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
$expression =~ s/<!--(.|\n)*-->//g;
@act=split(/=/,$string[1]);
$action=$act[1];


It works great!

Mouldy_Goat
09-25-2002, 05:25 PM
Just out of interest why aren't you using the CGI.pm module?

It comes as standard with the Perl distribution so you shouldn't have to fiddle about with installing it.

All you should have to do is:
#!/usr/bin/perl
use CGI qw(:standard);
print header;
print '<html><head><title>Welcome</title></head><body>';
print param('exp');
print '</body></html>';

FlashCoder
09-25-2002, 08:34 PM
I don't use the CGI.pm.
I do it:
#!/usr/bin/perl
print "Content-Type: text/html\n\n";

print '<html>...hello !!!';

Ok?

premshree
09-26-2002, 03:32 PM
It's just that I did not want to use any modules.
I got it working anyways, so that's fine.

I uploaded the file to tripod but it does not work.

Here's the URL :
http://qiksearch1.tripod.com/cgi-bin/infix-postfix.pl

Mouldy_Goat
09-26-2002, 04:41 PM
Could you post the source code so we can have a look..?

FlashCoder
09-26-2002, 09:57 PM
Originally posted by Mouldy_Goat
Could you post the source code so we can have a look..?

Do you want to know what the problem ? Or...???

premshree
09-27-2002, 03:58 PM
I tried the following using Tripod's script editor ( http://build.tripod.lycos.com/hpstudio/scripteditor/scripteditor.jsp ) :


#!/usr/local/bin/perl
#
# Author name:
# Creation date:
#
# Description:
#
printf "Hello";


See the error I get :
http://qiksearch1.tripod.com/cgi-bin/hello.pl

FlashCoder
09-27-2002, 04:16 PM
Originally posted by premshree
I tried the following using Tripod's script editor ( http://build.tripod.lycos.com/hpstudio/scripteditor/scripteditor.jsp ) :


#!/usr/local/bin/perl
#
# Author name:
# Creation date:
#
# Description:
#
printf "Hello";


See the error I get :
http://qiksearch1.tripod.com/cgi-bin/hello.pl

try it:


#!/usr/local/bin/perl
print "Content-Type: text/html\n\n";
#
# Author name:
# Creation date:
#
# Description:
#
print "Hello";


:)

premshree
09-27-2002, 06:12 PM
Hey thanks! Finally my PerlScript works on the web!

Check it out at :
http://qiksearch1.tripod.com/cgi-bin/infix-postfix.pl

Sample query :
http://qiksearch1.tripod.com/cgi-bin/infix-postfix.pl?expression=%28a%2Bb%29*c%2F%28d%5Ee%29&action=0

FlashCoder
09-27-2002, 07:18 PM
Originally posted by premshree
Hey thanks! Finally my PerlScript works on the web!

Check it out at :
http://qiksearch1.tripod.com/cgi-bin/infix-postfix.pl

Sample query :
http://qiksearch1.tripod.com/cgi-bin/infix-postfix.pl?expression=%28a%2Bb%29*c%2F%28d%5Ee%29&action=0

:)

premshree
09-28-2002, 01:12 PM
The Bandwidth of my site has exceeded......so in case you are interested in seeing the algorithms used, see :

http://www24.brinkster.com/premshree/infix-postfix/index.htm

:thumbsup:

FlashCoder
09-29-2002, 11:40 AM
Originally posted by premshree
The Bandwidth of my site has exceeded......so in case you are interested in seeing the algorithms used, see :

http://www24.brinkster.com/premshree/infix-postfix/index.htm

:thumbsup:

Cool !!!
:)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum