Reputation: 75
I've run into a rather odd problem. I'm simply trying to display comments the user has entered by looping through the arrays that store the name entered, and the commented associated with that name.
#Check through each name
for ( my $i = 0; $i < scalar @$namesRef; $i++ )
{
#Display the comment to the user
print @$namesRef[$i].": ".@$commentsRef[$i], p;
}
On the page where the comments are displays, rather than displaying them like 'John: comment' it displays it like 'Johncomment:'. Also, the ',p' is not included so the next comment doesn't go to a new line.
I would put up an image to better show what the problem is, but I don't have enough rep yet :/
EDIT: The @-signs are there since these are references to arrays outside of this subroutine.
EDIT: The initial arrays are declared in this subroutine.
sub buildForm { my $form = shift; #Check which button was pressed.
my $daysOld = 0; #Holds how many days old the user is.
my $commentErrors = 0;
my @names = ();
my @comments =(); #Array to store the user's comments.
my @errorMessages =(); #Array to store the error messages for the current form.
The following is where the subroutine for the comment form is called:
elsif ( $form == 3 )
{
&readComments(\@comments, \@names, \@errorMessages); #Read in the comments.
#Initial build - Can't have any errors.
&build3(\@comments,\@names, \@errorMessages, $commentErrors, param('name'), param('comment'));
}
elsif ( $form == 4 )
{
$commentErrors = &commentFormErrorCheck( param('name'), param('comment'), \@errorMessages ); #Check for an errors.
&build3(\@comments,\@names, \@errorMessages, $commentErrors, param('name'), param('comment'));
}
Upvotes: 2
Views: 127
Reputation: 4401
Generally I suggest using use strict
and use warnings
.
Then, when using CGI, you should remember that the output should be HTML where spaces are not always preserved.
Also looking at the "source code" in the browser (i.e. display HTML) can be helpful to find the bugs.
The problem description as it is is too abstract to provide a specific solution.
Upvotes: -2
Reputation: 1675
The main problem is the use of the @-signs in the print statement.
Assuming @names
and @comments
are parallel arrays, showing a full simplified example to demonstrate usage:
build3(\@comments, \@names);
sub build3 {
my $comments = shift;
my $names = shift;
for (my $i = 0; $i < @$names; $i++) {
print $names->[$i].": ".$comments->[$i], p;
}
}
That said, you might want to take a look at printf
to make that line more readable.
Also, don't forget about HTML escaping.
EDIT:
Adding an example using HTML escaping and printf()
.
use CGI qw/escapeHTML p/;
printf("%s: %s%s\n", escapeHTML($names->[$i]), escapeHTML($comments->[$i]), p);
Upvotes: 2