Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > accessing numeric variables from textfield input in a form

Reply
Thread Tools

accessing numeric variables from textfield input in a form

 
 
rallabs@adelphia.net
Guest
Posts: n/a
 
      11-22-2006
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;

 
Reply With Quote
 
 
 
 
attn.steven.kuo@gmail.com
Guest
Posts: n/a
 
      11-22-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:

(snipped)

> two
> messages appear in the error log, saying: "$side[0] not numeric in line
> 25" and "$side[1] not numeric in line 25".


(snipped)


> 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



You need an integer index to access a particular
element of the array @percentS. The value in
your $s variable is not an integer.

Was param($s) the integer you wanted?
If so, try:

$percentS[ param($s) ] = $pcnt;

instead of

$percentS[ $s ] = $pcnt;

--
Hope this helps,
Steven

 
Reply With Quote
 
 
 
 
rallabs@adelphia.net
Guest
Posts: n/a
 
      11-22-2006
(E-Mail Removed) wrote:

Was param($s) the integer you wanted?

--
Hope this helps,
Steven
Steven, Thanks very much, it DID help. Your insight helped me analyze
the situation in a more logical and sane way. I now realize that the
root of my problem is the necessity to use param() without any
arguments. This is because I haven't any idea how many textfields will
be presented for parsing. (my post contained a form with 2 textfields
only, but that's an oversimplification of the real situation, in which
the form presents as little as 1 and as many as 9 textfields.) When
one uses param with no arguments, one gets the NAMES of the textfields,
not their contents. The problem is how to get the contents. The
following is what I tried. Basically, I filled an array with the names
of the textfields and then tried to use param on each array element in
turn. I don't understand why it doesn't work, but I at least
understand why I was getting the error log msg. Here's the guts of my
script 'parse.cgi':

if (param()){
my @Parray=param();#these are the contents of textboxes in submitting
form, 1-9 of them
my $lenparr=scalar(@Parray);
print $cc->h2("The number of elements in \@Parray is $lenparr. \n");
foreach (0..$#Parray) {
print $cc->h3("\$Parray[$_] is $Parray[$_]. Content of checkbox#$_ is
param($Parray[$_]).\n");
}
The last operation, param($Parray[$_]) , does not yield the contents of
$Parray[$_], but rather the words " param(side[N] " for pass "N" thru
the foreach. Yukk. Thank you very much for the help.
Mike

> (E-Mail Removed) wrote:
>
> (snipped)
>
> > two
> > messages appear in the error log, saying: "$side[0] not numeric in line
> > 25" and "$side[1] not numeric in line 25".

>
> (snipped)
>
>
> > 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

>
>
> You need an integer index to access a particular
> element of the array @percentS. The value in
> your $s variable is not an integer.
>
> Was param($s) the integer you wanted?
> If so, try:
>
> $percentS[ param($s) ] = $pcnt;
>
> instead of
>
> $percentS[ $s ] = $pcnt;
>
> --
> Hope this helps,
> Steven


 
Reply With Quote
 
Amer Neely
Guest
Posts: n/a
 
      11-22-2006
(E-Mail Removed) wrote:
> (E-Mail Removed) wrote:
>
> Was param($s) the integer you wanted?
>
> --
> Hope this helps,
> Steven
> Steven, Thanks very much, it DID help. Your insight helped me analyze
> the situation in a more logical and sane way. I now realize that the
> root of my problem is the necessity to use param() without any
> arguments. This is because I haven't any idea how many textfields will
> be presented for parsing. (my post contained a form with 2 textfields
> only, but that's an oversimplification of the real situation, in which
> the form presents as little as 1 and as many as 9 textfields.) When
> one uses param with no arguments, one gets the NAMES of the textfields,
> not their contents. The problem is how to get the contents. The
> following is what I tried. Basically, I filled an array with the names
> of the textfields and then tried to use param on each array element in
> turn. I don't understand why it doesn't work, but I at least
> understand why I was getting the error log msg. Here's the guts of my
> script 'parse.cgi':
>
> if (param()){
> my @Parray=param();#these are the contents of textboxes in submitting
> form, 1-9 of them
> my $lenparr=scalar(@Parray);
> print $cc->h2("The number of elements in \@Parray is $lenparr. \n");
> foreach (0..$#Parray) {
> print $cc->h3("\$Parray[$_] is $Parray[$_]. Content of checkbox#$_ is
> param($Parray[$_]).\n");
> }
> The last operation, param($Parray[$_]) , does not yield the contents of
> $Parray[$_], but rather the words " param(side[N] " for pass "N" thru
> the foreach. Yukk. Thank you very much for the help.
> Mike
>
>> (E-Mail Removed) wrote:
>>
>> (snipped)
>>
>>> two
>>> messages appear in the error log, saying: "$side[0] not numeric in line
>>> 25" and "$side[1] not numeric in line 25".

>> (snipped)
>>
>>
>>> 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

>>
>> You need an integer index to access a particular
>> element of the array @percentS. The value in
>> your $s variable is not an integer.
>>
>> Was param($s) the integer you wanted?
>> If so, try:
>>
>> $percentS[ param($s) ] = $pcnt;
>>
>> instead of
>>
>> $percentS[ $s ] = $pcnt;
>>
>> --
>> Hope this helps,
>> Steven

>


According to 'Official Guide to Programming with CGI.pm' [Lincoln Stein]
this should walk through all the form fields:

foreach $name(param())
{
$value=param($name);
print "The value of $name is $value\n";
}

--
Amer Neely
w: www.softouch.on.ca/
b: www.softouch.on.ca/blog/
Perl | MySQL programming for all data entry forms.
"We make web sites work!"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Watir: problem accessing textfield property Mario Ruiz Ruby 0 02-18-2009 03:13 PM
int to numeric numeric(18,2) ? jobs ASP .Net 2 07-22-2007 12:32 AM
Arithmetic overflow error converting numeric to data type numeric. darrel ASP .Net 4 07-19-2007 09:57 PM
check if string contains numeric, and check string length of numeric value ief@specialfruit.be C++ 5 06-30-2005 01:08 PM
Textfield Numeric Sorting? Jared Cobb Javascript 2 09-18-2003 04:34 PM



Advertisments