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!

Equation in CGI

Status
Not open for further replies.

samesale

Programmer
Sep 15, 2003
133
US
I have place the following equation in my CGI program:

$total = $media/$count*100

Both $media and $count are already defined and has values. But the CGI will not accept it and gives error.
 
What error? Worked fine for me :

$media = 10 ;
$count = 2 ;

$total = $media/$count*100 ;

print $total ;


If your getting the illegal divide error then your not defining your variables like you think you are, I bet they are falling out of scope somewhere.
 
Thank you for your response. You are right it works as it is. However, here is my program and the program stops running when reaches the print "$tot1\n"; However, when I block out all the tot's equations, it runs again. Please help. Thanks.

#!/usr/bin/perl -w

use CGI;
$query = new CGI;
#pollr.cgi

$counti2=0;

print $query->header;

open(INF,"poll.out") or dienice("Couldn't open survey.out for reading: $! \n");

@data = <INF>;
close(INF);

# First we initialize some counters and hashes for storing the
# summarized data.

$commentary =&quot;&quot;;
$media1 =0;
$media2 =0;
$media3 =0;
$media4 =0;
$media5 =0;
$media6 =0;
$media7 =0;
$media8 =0;
$reason1 =0;
$reason2 =0;
$reason3 =0;
$reason4 =0;
$reason5 =0;
$opinion1 =0;
$opinion2 =0;
$opinion3 =0;
$opinion4 =0;
$opinion5 =0;
$opinion6 =0;
$opinion7 =0;
$commentt =0;

foreach $i (@data) {
chomp($i);
($media,$reason,$opinion,$comment,$month,$day,$year)=split(/\|/,$i);

# this is the same as $counti2 = $counti2 + 1;

$counti2++;

if ($media eq &quot;WUPS&quot;) { $media1 = $media1 +1};
if ($media eq &quot;WCZY&quot;) { $media2 = $media2 +1};
if ($media eq &quot;WMMI&quot;) { $media3 = $media3 +1};
if ($media eq &quot;news&quot;) { $media4 = $media4 +1};
if ($media eq &quot;Friend&quot;) { $media5 = $media5 +1};
if ($media eq &quot;salebyyou&quot;) { $media6 = $media6 +1};
if ($media eq &quot;billboard&quot;) { $media7 = $media7 +1};
if ($media eq &quot;other&quot;) { $media8 = $media8 +1};

if ($reason eq &quot;buy&quot;) { $reason1 = $reason1 +1};
if ($reason eq &quot;sell&quot;) { $reason2 = $reason2 +1};
if ($reason eq &quot;buysell&quot;) { $reason3 = $reason3 +1};
if ($reason eq &quot;curiosity&quot;) { $reason4 = $reason4 +1};
if ($reason eq &quot;other&quot;) { $reason5 = $reason5 +1};

if ($opinion eq &quot;inexeasy&quot;) { $opinion1 = $opinion1 +1};
if ($opinion eq &quot;inexhard&quot;) { $opinion2 = $opinion2 +1};
if ($opinion eq &quot;expeasy&quot;) { $opinion3 = $opinion3 +1};
if ($opinion eq &quot;exphard&quot;) { $opinion4 = $opinion4 +1};
if ($opinion eq &quot;Noopin&quot;) { $opinion5 = $opinion5 +1};
if ($opinion eq &quot;Confus&quot;) { $opinion6 = $opinion6 +1};
if ($opinion eq &quot;Other&quot;) { $opinion7 = $opinion7 +1};

if ($comment eq &quot;&quot;) { $commentt = $commentt + 1 };

print &quot;$count2i $comment<br>\n&quot;;
}

$tot1 = $media1/$count2i *100;
$tot2 = $media2/$count2i *100;
$tot3 = $media3/$count2i *100;
$tot4 = $media4/$count2i *100;
$tot5 = $media5/$count2i *100;
$tot6 = $media6/$count2i *100;
$tot7 = $media7/$count2i *100;
$tot8 = $media8/$count2i *100;

print &quot;$tot1\n&quot;;

# Now we can print out a web page summarizing the data.

print <<EndHTML;

<html><head><title>Survey Results</title></head><body>

<h2 align=CENTER>Survey Results</h2>

Total visitors: $counti2<p>
Total number of blank Comments: $commentt<p>
<table width=&quot;40%&quot; border=1><tr colspane=2><td width=&quot;40%&quot;>
How did you learn about this site?</td></tr>
<tr><td width=&quot;30%&quot; > WUPS </td><td width=&quot;10%&quot; > $media1</td> <td>$tot1</td> </tr>
<tr><td width=&quot;30%&quot; > WCZY </td><td width=&quot;10%&quot; > $media2</td> <td>$tot2</td></td> </tr>
<tr><td width=&quot;30%&quot; > WMMI </td><td width=&quot;10%&quot; > $media3</td> <td>$tot3</td></tr>
<tr><td width=&quot;30%&quot; > Newpaper </td><td width=&quot;10%&quot; > $media4</td> <td>$tot4</td></tr>
<tr><td width=&quot;30%&quot; > Friend </td><td width=&quot;10%&quot; > $media5</td> <td>$tot5</td></tr>
<tr><td width=&quot;30%&quot; > Contacted </td><td width=&quot;10%&quot; > $media6</td> <td>$tot6</td></tr>
<tr><td width=&quot;30%&quot; > Billboard </td><td width=&quot;10%&quot; > $media7</td> <td>$tot7</td></tr>
<tr><td width=&quot;30%&quot; > Other </td><td width=&quot;10%&quot; > $media8</td> <td>$tot8</td></tr> </table>
<br>
<table width=&quot;40%&quot; border=1><tr colspane=2><td width=&quot;40%&quot;>
Why did you visit the website?</td></tr>
<tr><td width=&quot;30%&quot;> Buy </td><td width=&quot;10%&quot;> $reason1</td></tr>
<tr><td width=&quot;30%&quot;> Sell </td><td width=&quot;10%&quot;> $reason2</td></tr>
<tr><td width=&quot;30%&quot;> Buy/Sell </td><td width=&quot;10%&quot;> $reason3</td></tr>
<tr><td width=&quot;30%&quot;> Curiosity </td><td width=&quot;10%&quot;> $reason4</td></tr>
<tr><td width=&quot;30%&quot;> Other </td><td width=&quot;10%&quot;> $reason5</td></tr> </table>
<br>
<table width=&quot;40%&quot; border=1><tr colspane=2><td width=&quot;40%&quot;>
What is your opinion of this website?</td></tr>

<tr><td width=&quot;30%&quot;> Inexpensive - Easy </td><td width=&quot;10%&quot;> $opinion1</td></tr>
<tr><td width=&quot;30%&quot;> Expensive - Easy </td><td width=&quot;10%&quot;> $opinion2</td></tr>
<tr><td width=&quot;30%&quot;> Inexpensive - Hard </td><td width=&quot;10%&quot;> $opinion3</td></tr>
<tr><td width=&quot;30%&quot;> Expensive - Hard </td><td width=&quot;10%&quot;> $opinion4</td></tr>
<tr><td width=&quot;30%&quot;> No Opinion </td><td width=&quot;10%&quot;> $opinion5</td></tr>
<tr><td width=&quot;30%&quot;> Confused </td><td width=&quot;10%&quot;> $opinion6</td></tr>
<tr><td width=&quot;30%&quot;> Other </td><td width=&quot;10%&quot;> $opinion7</td></tr>

</body></html>
EndHTML

sub dienice {
my($msg) = @_;
print &quot;<h2>Error</h2>\n&quot;;
print $msg;
exit;
}

 
You have a typo.

You are trying to use :

$count2i

in your equation but you are SETTING :

$counti2++;

Your i and your 2 are swapped. This typo prevents your calculation from ever being complete.
 
Thank you. I appreciate you finding my mistake. Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top