R
rallabs
Dear Perl Experts: A weird thing is happening to me when I try to use
numeric input from a textfield form. The following two scripts, named
'in_sides.cgi' and 'findhyp.cgi', illustrate the problem.
'findhyp' is supposed to do some math on the two input variables
from 'in_sides.cgi', and it does it successfully, but when I try to
store the two input variables into an array (line 25 of the script
'findhyp'), the resulting array has only one element says the
script, and it gives a nonsensical foreach result. In addition, two
messages appear in the error log, saying: "$side[0] not numeric in line
25" and "$side[1] not numeric in line 25". I only get these complaints
when I try to store the variables into the array, but not otherwise. I
have seen in several references, eg "Perl and CGI for the WWW" by
Elizabeth Castro, that string variables automatically convert to
numeric when used in an arithmetic expression, and so I tried on line
24 of the second script to add zero to each variable, but it didn't
work. The error message for line 25 refers to the variables as
'$side[0]' or '$side[1]' even though they are now called
'$pct'. If anybody can explain this I'll be forever grateful.
Thanks in advance.
Mike
The first script ('in_sides.cgi') is:
#!/usr/bin/perl -wT
#this is in_sides.cgi
use CGI::Carp qw(fatalsToBrowser);
use CGI qw
standard);
use strict;
use diagnostics;
use URI::Escape;
$CGI:
ISABLE_UPLOADS=1;
$CGI:
OST_MAX=500;
my $co = new CGI;
print $co->header("text/html");
print $co->start_html;
print $co->center($co->h1("This is where we input the two sides to the
right triangle\n"));
my @sidesarray;
$sidesarray[0]='a:';
$sidesarray[1]='b:';
print $co->startform(-method=>"POST",-action=>"findhyp.cgi");
foreach (0..$#sidesarray){
print $co->h3($sidesarray[$_]," ",textfield("side[$_]"));
}
print $co->p,$co->submit,$co->reset,
$co->endform;
exit;
and the second script ('findhyp.cgi') is:
#!/usr/bin/perl -wT
#This is findhyp.cgi
use strict;
use warnings;
use CGI qw
standard);
$CGI:
ISABLE_UPLOADS=1;
$CGI:
OST_MAX = 102_400; #100KB
my $cc = new CGI;
print $cc->header;
print $cc->start_html(-title=>'findhyp');
my @percentS;
my $sumsq=0.0;
if (param()){
my @param=param();#these are the two numbers from textboxes on previous
page
foreach my $s (param () ) {
my $ss=param($s);
print $cc->h2("The variable \$s is $ss\n");
$sumsq=$sumsq + param($s)*param($s); #sums up squares of two sides
}
my $squirt=sqrt($sumsq);
print $cc->h2("The variable \$sumsq is $sumsq\n");
print $cc->h2("The variable \$squirt is $squirt\n");
foreach my $s ( param() ) {
my $pcnt=0+param($s);# this is supposed to make the $pcnt variable
numeric according to Castro p24
$percentS[$s] =$pcnt; #we're getting 2 messages in the errorlog saying
"$side[n] not numeric in line 25"; one for each pass thru the foreach
}
}
my $numelements=scalar(@percentS);
print $cc->h2("The number of elements in array \@percentS is
$numelements\n");
print $cc->h2("The index number of the last element is $#percentS\n");
exit;
numeric input from a textfield form. The following two scripts, named
'in_sides.cgi' and 'findhyp.cgi', illustrate the problem.
'findhyp' is supposed to do some math on the two input variables
from 'in_sides.cgi', and it does it successfully, but when I try to
store the two input variables into an array (line 25 of the script
'findhyp'), the resulting array has only one element says the
script, and it gives a nonsensical foreach result. In addition, two
messages appear in the error log, saying: "$side[0] not numeric in line
25" and "$side[1] not numeric in line 25". I only get these complaints
when I try to store the variables into the array, but not otherwise. I
have seen in several references, eg "Perl and CGI for the WWW" by
Elizabeth Castro, that string variables automatically convert to
numeric when used in an arithmetic expression, and so I tried on line
24 of the second script to add zero to each variable, but it didn't
work. The error message for line 25 refers to the variables as
'$side[0]' or '$side[1]' even though they are now called
'$pct'. If anybody can explain this I'll be forever grateful.
Thanks in advance.
Mike
The first script ('in_sides.cgi') is:
#!/usr/bin/perl -wT
#this is in_sides.cgi
use CGI::Carp qw(fatalsToBrowser);
use CGI qw
use strict;
use diagnostics;
use URI::Escape;
$CGI:
$CGI:
my $co = new CGI;
print $co->header("text/html");
print $co->start_html;
print $co->center($co->h1("This is where we input the two sides to the
right triangle\n"));
my @sidesarray;
$sidesarray[0]='a:';
$sidesarray[1]='b:';
print $co->startform(-method=>"POST",-action=>"findhyp.cgi");
foreach (0..$#sidesarray){
print $co->h3($sidesarray[$_]," ",textfield("side[$_]"));
}
print $co->p,$co->submit,$co->reset,
$co->endform;
exit;
and the second script ('findhyp.cgi') is:
#!/usr/bin/perl -wT
#This is findhyp.cgi
use strict;
use warnings;
use CGI qw
$CGI:
$CGI:
my $cc = new CGI;
print $cc->header;
print $cc->start_html(-title=>'findhyp');
my @percentS;
my $sumsq=0.0;
if (param()){
my @param=param();#these are the two numbers from textboxes on previous
page
foreach my $s (param () ) {
my $ss=param($s);
print $cc->h2("The variable \$s is $ss\n");
$sumsq=$sumsq + param($s)*param($s); #sums up squares of two sides
}
my $squirt=sqrt($sumsq);
print $cc->h2("The variable \$sumsq is $sumsq\n");
print $cc->h2("The variable \$squirt is $squirt\n");
foreach my $s ( param() ) {
my $pcnt=0+param($s);# this is supposed to make the $pcnt variable
numeric according to Castro p24
$percentS[$s] =$pcnt; #we're getting 2 messages in the errorlog saying
"$side[n] not numeric in line 25"; one for each pass thru the foreach
}
}
my $numelements=scalar(@percentS);
print $cc->h2("The number of elements in array \@percentS is
$numelements\n");
print $cc->h2("The index number of the last element is $#percentS\n");
exit;