Re: what is wrong with my script



Chen Li wrote:
Hi guys,

I have a CGI script to process some data from the
visitors. The input data takes this format(row x
column):
1 2 4 5 6 7
100 90 50 30 20 0

What do you mean by this? How is in the input given to your script?
Do you mean that the user enters this in a <textarea> box, and the user
specifically puts a tab after each number, and a newline after the 7?

After processing I want to print out the result in the
same format(row x column). But when I ran my script I
only get a row only:

1 2 4 5 6 7 100 90 50 30 20 0

It looks like all lines are changed into a string
only.

That is non sensical. A "line" is a string of text terminated with a
newline. I have no idea what you mean by a line being changed into a
string.


here is the code:

#!c:/Perl/bin/perl.exe

use warnings;
use strict;
use CGI qw/:standard/;

Here you tell Perl you want to use the functional approach to CGI...

###################create forms
my $q=CGI->new();

....but here you use the object-oriented approach. Make up your mind.


print

$q->header,
$q->start_html('Survival Curve'),
$q->h1('Survival Curve'),
$q->start_form(),
$q->textarea( -name=>'data',
-default=>'',
-rows=>10,
-columns=>50),
$q->p,
$q->submit('Submit'),
$q->end_form();



######################process the data
my @data=();
my $cgi=CGI->new();
my $line=$cgi->param('data');

So $line contains the text that was entered into the <textarea> box.
$line is one string. It contains text, which may or may not contain
newlines (depending on whether or not the user entered a newline in the
textarea box).

if ($line ne '' ){

my @temp=split/\t+/,$line;

So now @temp is a sequence of values that were separated by a tab.
Note that a newline is not a tab. So if your user had entered your
format, then $line would be:

"1\t2\t4\t5\t6\t7\n100\t90\t50\t30\t20\t0"

And therefore, @temp is now:

("1", "2", "4", "5", "6", "7\n100", "90", "50", "30", 20", "0")


push @data,[@temp];

Now you're pushing a reference to an anonymous array that contains
those above values into @data. Note that this is the *only* thing you
ever push into @data. @data contains exactly ONE element - a reference
to this single array.

}

for my $ref(@data){

Now you're trying to loop through @data, which of course only contains
one element. So this loop iterates exactly once, and assigns $ref to
be that array reference you pushed into @data.

print "\t@$ref","\n";

Here you print a single tab, followed by all the values that were in
@temp (all squished together), followed by a newline. Note that you
never print any break tags, so the newlines are discarded when your
HTML is rendered in your browser.

print $cgi->p;
}

print $q->end_html();

exit;

I have no idea what you were *expecting* to be in @temp, @data, or
$ref, so it's difficult to help you out. I hope my analysis of your
program at least helps you to form a more specific question.

Paul Lalli

.



Relevant Pages