0
votes

i am new to Perl and using Perl in my back end script and HTML in front end and CGI framework . Initially i am reading some details from flat file and displaying them . I am then trying to print the details in the form of checkboxes. i am using use Data::Dumper; module to check the values . however my input value of the last checkbox has an extra space as shown below

 print '<form action="process.pl " method="POST" id="sel">';
 print '<input type="checkbox" onClick="checkedAll()">Select All<br />';
 foreach my $i (@robos) {
    print '<input type="checkbox" name="sel" value="';
    print $i;
    print '">';
    print $i;
    print '<br />';
}
 print '<input type="submit" value="submit">';
 print '</form>';

 print "response " . Dumper \$data;

however in Process.pl the selected value is retrieved using

   @server = $q->param('sel');

but the response of selection is

 print "response " . Dumper \@server; => is showing [ 'ar', 'br', 'cr ' ]; which is 

showing an additional space after cr . I dont know whats is wrong .

In my flat file i have

RMCList:ar:br:cr 

i am then reading the details into an array and splitting using

  foreach my $ln (@lines) 
  { 
  if ($ln =~ /^RMCList/) 
 { 
@robos = split (/:/,$ln);
 }
 } #### end of for statement 
shift (@robos); 
print "The robos are ", (join ',', map { '"' . $_ . '"' } @robos), "\n";

This is showing the robos are:

"ar","br","cr
"
1
Presumably, that space is already present inside @robos – have you tried looking at that data? Note that you should escape your dumps: use HTML::Entities;, then print encode_entities Dumper \@robos.amon
yes i have looked on the content of @robos by print "the robos are @robos\n"; how will i come to know whether there is an extra space after last elementuser3095218
By putting quotes around each element: print "The robos are ", (join ',', map { '"' . $_ . '"' } @robos), "\n"amon
its showing a space for print encode_entities Dumper \@robos $VAR1 = [ 'ar', 'br', 'cr ' ];user3095218
i dont understand its still showing a space . i am parsing the list from a flat file . i checked the flat file there is no space after cr.user3095218

1 Answers

1
votes

While reading the file into @lines, you forgot to remove the newline. This newline is interpreted as a simple space in a HTML document.

Remove the newlines with the chomp function like

chomp @lines;

before looping over the lines.


Actually, don't read the file into an array at all, unless you have to access each line more than once. Otherwise, read one line at the time:

open my $infile, "<", $filename or die "Cannot open $filename: $!";
my @robos;
while (my $ln = <$infile>) {
    chomp $ln;

    @robos = split /:/, $ln if $ln =~ /^RMCList/;
}