PDA

View Full Version : Perl/CGI n00b Question... regarding Strings


RandomGuy
11-23-2005, 07:42 AM
Ok I have just started rolling on the path to learning Perl/CGI. I have intermediate programming skills in other languages, this is starting to grow on me as far as usability of my apps go.

Here is my question. Say there was a page called blah.html with a form in it that has an action that feeds to a script in the cgi-bin directory called 'blah.cgi' right?
This form contains 3 text input boxes, named T1, T2, T3.

How can I call the input strings from the boxes individually?
open(OUT, ">../display.html") or &dienice("Couldn't open output file: $!");
print header;
print start_html("Schedule Changer");
print h2("Schedule Update Completed");
my %form;
foreach my $p (param()) {
$form{$p} = param($p);
print OUT " $form{$p} ";
}
print end_html;
close(OUT);

that is my code from a tutorial that i modified, it seems to work, but due to the foreach loop, I can not get any of the data individually. Say I wanted to determine what was in the first box, what was in the second and what was in the third... (in my own coding... without using loops and what not... how would I accomplish this?)
also
How would I make it print to the file only the contents of the second box?

etc. I have strong VB background, so this is kind of wierd to me.

To simply restate my question. How can I load the data of EACH box into a seperate string, or load the info into an array, where I can easily use the data from the boxes.
example
Print $InputBox1;

Thanks alot.

nkrgupta
11-23-2005, 08:08 AM
my %form;
foreach my $p (param()) {
$form{$p} = param($p);
print OUT " $form{$p} ";
}

The above code is dynamically creating a hash or an associative array of all the values which are being submitted through the form. To grab the values indiviually you can replace the above code with the following

use CGI;

$q=new CGI;

$value1=$q->param("T1");
$value2=$q->param("T2");
$value3=$q->param("T3");

Now you have the values of the 3 text boxes in three different variables and you can do whatever you want with any of them.

print OUT $value1;

You may want to have a look at http://codingforums.com/showthread.php?t=72360 for Perl tutorials.

Naveen

FishMonger
11-23-2005, 09:12 AM
use strict;
use CGI qw(:cgi-lib);

my %form = Vars;

print $form{'T1'},$/;
print $form{'T2'},$/;
print $form{'T3'},$/;

oruse strict;
use CGI;

my $q = new CGI;
my %form = $q->Vars;

print $form{'T1'},$/;
print $form{'T2'},$/;
print $form{'T3'},$/;

or print in a loopprint $form{$_},$/ for ('T1', 'T2', 'T3');

RandomGuy
11-23-2005, 07:48 PM
i really appreciate the help guys.
Here is what I ended out with my (@rf, @rsf, @rss, @rsuf, @rsus);
push(@rf,param("rf1"), param("rf2"), param("rf3"), param("rf4"));
push(@rsf,param("rsf1"), param("rsf2"), param("rsf3"), param("rsf4"));
push(@rss,param("rss1"), param("rss2"), param("rss3"), param("rss4"));
push(@rsuf,param("rsuf1"), param("rsuf2"), param("rsuf3"), param("rsuf4"));
push(@rsus,param("rsus1"), param("rsus2"), param("rsus3"), param("rsus4"));
my (@mgf, @mgsf, @mgss, @mgsuf, @mgsus);
push(@mgf,param("mgf1"), param("mgf2"), param("mgf3"), param("mgf4"));
push(@mgsf,param("mgsf1"), param("mgsf2"), param("mgsf3"), param("mgsf4"));
push(@mgss,param("mgss1"), param("mgss2"), param("mgss3"), param("mgss4"));
push(@mgsuf,param("mgsuf1"), param("mgsuf2"), param("mgsuf3"), param("mgsuf4"));
push(@mgsus,param("mgsus1"), param("mgsus2"), param("mgsus3"), param("mgsus4"));
my (@kf, @ksf, @kss, @ksuf, @ksus);
push(@kf,param("kf1"), param("kf2"), param("kf3"), param("kf4"));
push(@ksf,param("ksf1"), param("ksf2"), param("ksf3"), param("ksf4"));
push(@kss,param("kss1"), param("kss2"), param("kss3"), param("kss4"));
push(@ksuf,param("ksuf1"), param("ksuf2"), param("ksuf3"), param("ksuf4"));
push(@ksus,param("ksus1"), param("ksus2"), param("ksus3"), param("ksus4"));
my (@dmf, @dmsf, @dmss, @dmsuf, @dmsus);
push(@dmf,param("dmf1"), param("dmf2"), param("dmf3"), param("dmf4"));
push(@dmsf,param("dmsf1"), param("dmsf2"), param("dmsf3"), param("dmsf4"));
push(@dmss,param("dmss1"), param("dmss2"), param("dmss3"), param("dmss4"));
push(@dmsuf,param("dmsuf1"), param("dmsuf2"), param("dmsuf3"), param("dmsuf4"));
push(@dmsus,param("dmsus1"), param("dmsus2"), param("dmsus3"), param("dmsus4"));

There is a LOT of input i need, approximately 80 different text fields on the form. Is there any more efficient way to do it, or should this suffice?

also
I am wanting to print this into an HTML file, into a specific table... each set of info, here is the code I'm using for each print
open(OUT, ">../schedule.html") or &dienice("Couldn't open output file: $!");
print header;
print start_html("Schedule Changer");
print h2("Schedule Update Completed");
my $i;
for ($i = 0; $i < 4; $i++) {
print OUT @mgf[$i], " 5 pm <br> ";
}

print end_html;
close(OUT);
I want to print a whole block of HTML code to the HTML file, then print 1 variable worth of data into a table cell, close the cell tag, then print another array into another cell, close the cell tag, and after I'm done with all variables, close the HTML file with </body> and </html>

FishMonger
11-23-2005, 10:07 PM
use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser);

my $q = new CGI;
my %form = $q->Vars;

open(OUT, ">../schedule.html") or die("Couldn't open output file: $!");

print $q->header;
print $q->start_html("Schedule Changer");
print $q->h2("Schedule Update Completed");

for (1..4) { print OUT $form{"mgf$_"} . " 5 pm <br>\n"; }

FishMonger
11-23-2005, 10:22 PM
foreach my $param qw(mgf mgsf mgss mgsuf mgsus) {
for (1..4) { print OUT $form{"$param$_"} . " 5 pm <br>\n"; }
}
EDIT: In case you didn't catch what I'm suggesting, you should drop the use of those arrays. They only server to make the code very klunky and messy. As you can see from this example, it's very easy to loop through a subset of the hash elements.

RandomGuy
11-23-2005, 11:58 PM
foreach my $param qw(mgf mgsf mgss mgsuf mgsus) {
for (1..4) { print OUT $form{"$param$_"} . " 5 pm <br>\n"; }
}
EDIT: In case you didn't catch what I'm suggesting, you should drop the use of those arrays. They only server to make the code very klunky and messy. As you can see from this example, it's very easy to loop through a subset of the hash elements.
i dont catch it?... if i dont use the arrays, then how exactly would i assign the values to variables? I find the push statement easiest.

FishMonger
11-24-2005, 12:23 AM
Unless I misunderstood your prior explaination of what you're doing, you're receiving the values from the form submission. If that's the case, you'd use the CGI module to retrieve and load them into the vars i.e.,

my $q = new CGI;
my %form = $q->Vars;

Now, all 80 of your form field values are in the %form hash and you'd use a foreach loop like I showed to output them, so there is no need to use the arrays.

If I missunderstood, please explain where the values are comming from so I can work out the best method to assign the vars.

RandomGuy
11-24-2005, 03:50 AM
Unless I misunderstood your prior explaination of what you're doing, you're receiving the values from the form submission. If that's the case, you'd use the CGI module to retrieve and load them into the vars i.e.,

my $q = new CGI;
my %form = $q->Vars;

Now, all 80 of your form field values are in the %form hash and you'd use a foreach loop like I showed to output them, so there is no need to use the arrays.

If I missunderstood, please explain where the values are comming from so I can work out the best method to assign the vars.
cool..
ok say if i were to create that hash... how exactly would i call the information in them... like to get a value. is it an index type of thing?

FishMonger
11-24-2005, 04:12 AM
Hashes are string indexed.

print $form{'rf1'};

would print the same as your array

print $rf[0];

See my prior examples.

I need to go and entertain 35 people for the next few days for Thanksgiving events, but I'll check-in when I can.

RandomGuy
11-24-2005, 06:49 AM
awesome.... thanks TOO much for all the help you've given. Seriously. Have a Happy Holiday