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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Strings

Status
Not open for further replies.

hunter13605

Technical User
Apr 7, 2006
44
US
I am writing a program to find the mode of a group of numbers. I want to send a string of numbers from gerdata.pl to save.pl.

This is the form code from getdata.pl
#-----------------------------------------------------------
print"<TABLE>
<FORM ACTION=save.pl METHOD='post'>
<INPUT TYPE='hidden' NAME='size' VALUE=$size>";

for($i = 1; $i <= ($size); ++$i)
{
print "
<TR BGCOLOR='beige' valign='top'>
<TD><FONT FACE='arial' SIZE='2' COLOR='black'><div align='left'>$i</TD>
<TD><FONT FACE='arial' SIZE='2' COLOR='black'><div align='left'><INPUT TYPE='textbox' SIZE='5' NAME='number[$i]' VLAUE=$number[$i]></TD>
</TR>";
}

print "</TABLE>";
print "<INPUT TYPE='submit' VALUE='FIND RESULTS'>";
print "</FORM>";
#----------------------------------------------------------

This is the code that i am using to recieve the string. It dosen't appear to work.
#-----------------------------------------------------------
$size= $query->param('size');
for($i = 1; $i <= ($size); ++$i)
{
$number[$i]= $query->param('number[$i]');
}
#-----------------------------------------------------------

I am getting the number of numbers on the string and then using a for loop to recieve the numbers. In my mind and in c++ files that i have written it would seem that it should work perfectly but i can't seem to get it to work. Please help.
 
hmmm.... can't figure out what you are trying to do. Must be the Friday afternoon want-to-go-out-and-party blues.


What is ($size) supposed to be?


for($i = 1; $i <= ($size); ++$i)

and where does $number[$i] come from?


lines like these won't work:

$number[$i]= $query->param('number[$i]');

because you have $i inside of single-quotes which kills variable interpolation.
 
Basically, I am getting a list of number (The length defined by size) and i want to save this list to a text file. Is there a way that i can tell the form to send them to that text file.

I am trying to write a program that will take in a list of numbers and return the mode. I know it is easy to do it with just looking at the list with my own eyes but the assignement was to write a program that did it.

You can see what i have done at
 
bad link you posted but I figured it out. Like I said, the single-quotes are killing the variable interpolation. Try this:

Code:
$size= $query->param('size');
for($i = 1; $i <= ($size); ++$i)
  {
   $number[$i]= $query->param("number[$i]");
  }

but that is an awkward syntax "number[$i]" because it so closely resembles $number[$i], it looks like a syntax error. I would dispense with the number[] part of the name attribute and just use "number". You can use CGI.pm to retrieve the number fields as a list:

my @numbers = $query->param('number');
 
Thanks for the help. The whole world of perl is new to me so i am still learning. I see what you mean. That does look like a syntax error to me also. I have gotten a lot of them. I have seen the '@' symbol used before in different code i have looked at but never understood what it did. Could you please explain what CGI.pm means and what the @ symbol does
 
In your code you have:

$query->param('size');

this means you must already be using the CGI.pm module, something like:

use CGI;
$query = new CGI;

if not, your script will error out with something like:

Undefined subroutine &main::param called at.......

CGI.pm is a core perl module used for parsing CGI form data and generating HTML code and some more:


it's a big module and does lots of things and the documentation can be confusing at first but take it a little at a time and you will understand how to use it.

Perl has three basic data types:

$string (scalar)
@array (array: a list of scalars)
%hash (hash: a special array with key/value pairs)
 
I do use those two lines in every file
Code:
use CGI;
$query = new CGI;
I got a book that said that you need to do that at the beginning of every file.

I am guessing that i want to use an array. I have been working with c++ for the last 2 years and have found that there are many similarities between the two.

So what i need to know is:
1. do i need to sent the size of the array to the save file?
2. in the form how would i send the array? This is the form code that i have right now.
Code:
print"<TABLE>
  <FORM ACTION=save.pl METHOD='post'>
  <INPUT TYPE='hidden' NAME='size' VALUE=$size>";
  
  for($i = 1; $i <= ($size); ++$i)
  {
print "
   <TR BGCOLOR='beige' valign='top'>
   <TD><FONT FACE='arial' SIZE='2' COLOR='black'><div align='left'>$i</TD>
   <TD><FONT FACE='arial' SIZE='2' COLOR='black'><div align='left'><INPUT TYPE='textbox' SIZE='5' NAME='number[$i]' VLAUE=$number[$i]></TD>
  </TR>";
  }
  
print "</TABLE>";
print "<INPUT TYPE='submit' VALUE='FIND RESULTS'>";
print "</FORM>";
 
you can do this to print the form:

Code:
print"<TABLE>
  <FORM ACTION='save.pl' METHOD='post'>
  <INPUT TYPE='hidden' NAME='size' VALUE=$size>";
  
  for my $i (1 .. $size)
  {
print "
   <TR BGCOLOR='beige' valign='top'>
   <TD><FONT FACE='arial' SIZE='2' COLOR='black'><div align='left'>$i</TD>
   <TD><FONT FACE='arial' SIZE='2' COLOR='black'><div align='left'><INPUT TYPE='textbox' SIZE='5' [b]NAME='number'[/b] [b]VALUE='$i'[/b]></TD>
  </TR>";
  }
  
print "</TABLE>";
print "<INPUT TYPE='submit' VALUE='FIND RESULTS'>";
print "</FORM>";

Then in save.pl:

Code:
use CGI;
use strict;
my $query = new CGI;
my @numbers = $query->param('number');
my $sum_of_numbers = 0;
foreach my $number (@numbers) {
   $sum_of_numbers += $number;
}
print $query->header,$query->start_html;
print "The sum of the numbers is: $sum_of_numbers";
print $query->end_html;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top