How do I convert CSV into an HTML table using Perl? - html

In my code, I want to view all data from a CSV in table form, but it only displays the last line. How about lines 1 and 2? Here's the data:
1,HF6,08-Oct-08,34:22:13,df,jhj,fh,fh,ffgh,gh,g,rt,ffgsaf,asdf,dd,yoawa,DWP,tester,Pattern
2,hf35,08-Oct-08,34:12:13,dg,jh,fh,fgh,fgh,gh,gfh,re,fsaf,asdf,dd,yokogawa,DWP,DWP,Pattern
3,hf35,08-Oct-08,31:22:03,dg,jh,fh,fgh,gh,gh,gh,rte,ffgsaf,asdf,dfffd,yokogawa,DWP,DWP,ghh
Here's the code:
#! /usr/bin/perl
print "Content-type:text/html\r\n\r\n";
use CGI qw(:standard);
use strict;
use warnings;
my $line;
my $file;
my ($f1,$f2,$f3,$f4,$f5,$f6,$f7,$f8,$f9,$f10,$f11,$f12,$f13,$f14,$f15,$f16,$f17,$f18,$f19);
$file='MyFile.txt';
open(F,$file)||die("Could not open $file");
while ($line=<F>)
{
($f1,$f2,$f3,$f4,$f5,$f6,$f7,$f8,$f9,$f10,$f11,$f12,$f13,$f14,$f15,$f16,$f17,$f18,$f19)= split ',',$line;
}
close(F);
print "<HTML>";
print "<head>";
print "<body bgcolor='#4682B4'>";
print "<title>FUSION SHIFT REPORT</title>";
print "<div align='left'>";
print "<TABLE CELLPADDING='1' CELLSPACING='1' BORDER='1' bordercolor=black width='100%'>";
print "<TR>";
print "<td width='12%'bgcolor='#00ff00'><font size='2'>RECORD No.</td>";
print "<td width='12%'bgcolor='#00ff00'><font size='2'>TESTER No.</td>";
print "<td width='12%'bgcolor='#00ff00'><font size='2'>DATE</td>";
print "<td width='13%'bgcolor='#00ff00'><font size='2'>TIME</td>";
print "<td width='11%'bgcolor='#00ff00'><font size='2'>DEVICE NAME</td>";
print "<td bgcolor='#00ff00'><font size='2'>TEST PROGRAM</td>";
print "<td bgcolor='#00ff00'><font size='2'>DEVICE FAMILY</td>";
print "<td width='13%'bgcolor='#00ff00'><font size='2'>SMSLOT</td>";
print "<td width='13%'bgcolor='#00ff00'><font size='2'>DIE LOT</td>";
print "<td width='12%'bgcolor='#00ff00'><font size='2'>LOADBOARD</td>";
print "<td width='12%'bgcolor='#00ff00'><font size='2'>TESTER </td>";
print "<td width='12%'bgcolor='#00ff00'><font size='2'>SERIAL NUMBER</td>";
print "<td width='13%'bgcolor='#00ff00'><font size='2'>TESTER CONFIG</td>";
print "<td width='11%'bgcolor='#00ff00'><font size='2'>SMSLOT</td>";
print "<td bgcolor='#00ff00'><font size='2'>PACKAGE</td>";
print "<td bgcolor='#00ff00'><font size='2'>SOCKET</td>";
print "<td width='13%'bgcolor='#00ff00'><font size='2'>ROOT CAUSE 1</td>";
print "<td width='13%'bgcolor='#00ff00'><font size='2'>ROOT CAUSE 2</td>";
print "<td width='13%'bgcolor='#00ff00'><font size='2'>ROOT CAUSE 3</td>";
print "</tr>";
print "<TR>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f1</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f2</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f3</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f4</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f5</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f6</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f7</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f8</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f9</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f10</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f11</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f12</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f13</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f14</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f15</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f16</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f17</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f18</TD>";
print "<TD bgcolor='#ADD8E6'><font size='2'>$f19</TD>";
print "</tr>";
print "</TABLE>";
print "</body>";
print "<html>";

You need to output the table rows inside the while loop, as that's where you are reading the lines.
So change the code so that it
outputs table headers
reads the file line by line outputting table rows
outputs table footer
Here's how your loop might look if a little simplified...
while ($line=<F>)
{
print "<tr>";
my #cells= split ',',$line;
foreach my $cell (#cells)
{
print "<td>$cell</td>";
}
print "</tr>";
}

HTML::Template would make your life a lot easier. Here's my go with a cut-down template.
#!/usr/local/bin/perl
use strict;
use warnings;
use HTML::Template;
my #table;
while (my $line = <DATA>){
chomp $line;
my #row = map{{cell => $_}} split(/,/, $line);
push #table, {row => \#row};
}
my $tmpl = HTML::Template->new(scalarref => \get_tmpl());
$tmpl->param(table => \#table);
print $tmpl->output;
sub get_tmpl{
return <<TMPL
<html>
<TMPL_LOOP table>
<tr>
<TMPL_LOOP row>
<td><TMPL_VAR cell></td></TMPL_LOOP>
</tr></TMPL_LOOP>
</html>
TMPL
}
__DATA__
1,HF6,08-Oct-08,34:22:13,df,jhj,fh,fh,ffgh,gh,g,rt,ffgsaf,asdf,dd,yoawa,DWP,tester,Pattern
2,hf35,08-Oct-08,34:12:13,dg,jh,fh,fgh,fgh,gh,gfh,re,fsaf,asdf,dd,yokogawa,DWP,DWP,Pattern
3,hf35,08-Oct-08,31:22:03,dg,jh,fh,fgh,gh,gh,gh,rte,ffgsaf,asdf,dfffd,yokogawa,DWP,DWP,ghh

$f1,$f2,$f3,$f4
Any time you see code like that alarm bells should go off. Use an array.

Allow me to demonstrate with a smaller example.
my $f;
while ($line = <F>) {
$f = $line;
}
print $f;
The above code will read every line of the file F, assigning each line to the variable $f. Each time it assigns a new line, the previous line is overwritten. When it reaches the end of the file, it prints out the value of $f once.
my $f;
while ($line = <F>) {
$f = $line;
print $f;
}
The above code has the print inside the loop, so the print will run for each line of the input. You will want to make a similar modification to your code to get the output you expect.

The reason you were only seeing the last line was that you ran the print's after reading the entire file and discarding each line when the next was read.
To be honest, there's various things wrong with that code. I'll mention a couple and address those in the code snippet below.
Don't parse CSV data yourself (the split). Use Text::CSV or -- if you need better performance -- Text::CSV_XS.
Your HTML is invalid. You don't close all tags and in particular, title should be in head, but not body.
You should use a templating module for inserting tabular data into a web page. Examples: HTML::Template or Template Toolkit.
At the very least, use functions to abstract out the redundant code.
Better yet, use the fact that the formatting of the data is also data. That's why it should be treated as such.
If you ever find that you're declaring lots of variables with numbers attached ($f1,$f2,...), you really want an array: #fields = split /,/, $line;
use strict;
use warnings;
use CGI qw();
use Text::CSV;
my $cgi = CGI->new();
print $cgi->header();
my $file ='MyFile.txt';
# should really use a templating module from CPAN,
# but let's take it step by step.
# Any of the following would fit nicely:
# - Template.pm (Template Toolkit)
# - HTML::Template, etc.
my $startHtml = <<'HERE';
<html>
<head> <title>FUSION SHIFT REPORT</title> </head>
<body bgcolor="#4682B4">
<div align="left">
<table cellpadding="1" cellspacing="1" border="1" bordercolor="black" width="100%">
HERE
my $endHtml = <<'HERE';
</table>
</body>
<html>
HERE
my #columns = (
{ name => 'RECORD No.', width => 12 },
{ name => 'TESTER No.', width => 12 },
{ name => 'DATE', width => 12 },
{ name => 'TIME', width => 13 },
{ name => 'DEVICE NAME', width => 11 },
{ name => 'TEST PROGRAM' },
{ name => 'DEVICE FAMILY' },
{ name => 'SMSLOT', width => 13 },
{ name => 'DIE LOT', width => 13 },
{ name => 'LOADBOARD', width => 12 },
{ name => 'TESTER', width => 12 },
{ name => 'SERIAL NUMBER', width => 12 },
{ name => 'TESTER CONFIG', width => 13 },
{ name => 'SMSLOT', width => 11 },
{ name => 'PACKAGE' },
{ name => 'SOCKET' },
{ name => 'ROOT CAUSE 1', width => 13 },
{ name => 'ROOT CAUSE 2', width => 13 },
{ name => 'ROOT CAUSE 3', width => 13 },
);
my $csv = Text::CSV->new();
open my $fh, '<', $file
or die "Could not open file '$file': $!"; # should generate a HTML error here
# print header
print $startHtml;
print_table_header(\#columns);
while (defined(my $line = <$fh>)) {
$csv->parse($line);
# watch out: This may be "tainted" data!
my #fields = $csv->fields();
#fields = #fields[0..$#columns] if #fields > #columns;
print_table_line(\#fields);
}
close $fh;
print $endHtml;
sub print_table_header {
my $columns = shift;
print "<tr>\n";
foreach my $column (#$columns) {
my $widthStr = (defined($column->{width}) ? ' width="'.$column->{width}.'"' : '');
my $colName = $column->{name};
print qq{<td$widthStr bgcolor="#00FF00"><font size="2">$colName</font></td>\n};
}
print "</tr>\n";
}
sub print_table_line {
my $fields = shift;
print "<tr>\n";
foreach my $field (#$fields) {
print qq{<td bgcolor=\"#ADD8E6\"><font size="2">$field</font></td>\n};
}
print "</tr>\n";
}

Please close your <font> tags. Just because the browser will handle their lack doesn't mean they're not valuable to include.

This isn't the most robust solution, matter of fact is has some pretty nasty failures if you have commas inside values, but it did the job for me:
(CSV data is in a variable called $content)
$content =~ s#\n#</td></tr><tr><td>#g;
$content =~ s#,#</td><td>#g;
$content = "<table><tr><td>$content</td></tr></table>";

Related

convert text file to html ouput so that data can be printed in table format

I have a text file which has 3 values separated by :
25-08-2019_19.00.00 : Port port1 of URL http://ip1:port1/ is NOT OPEN : Zoom1
25-08-2019_19.00.00 : Port port2 of URL http://ip2:port2/ is NOT OPEN : MP
and so on.
I want to print the output to a html type tabular format file, which has 3 headings, date, output and system and corresponding data in 3 columns.
I tried below code, but it is not putting data to table format.
#! /bin/bash
if [ $# -eq 0 ] ; then
echo "USAGE: $(basename $0) file1 file2 file3 ..."
exit 1
fi
for file in $* ; do
html=$(echo $file | sed 's/\.txt$/\.html/i')
echo "<html>" >> $html
echo "<style type="text/css">
table, th, td {
border: 1px solid black;
}
</style>" >> $html
echo " <body>" >> $html
echo '<table>' >> $htm
echo '<th>HEADING1</th>' >> $html
echo '<th>HEADING2</th>' >> $html
echo '<th>HEADING3</th>' >> $html
while IFS=':' read -ra line ; do
echo "<tr>" >> $html
for i in "${line[#]}"; do
echo "<td>$i</td>" >> $html
# echo "<td>$i</td>" >> $html
done
echo "</tr>" >> $html
done < $file
echo '</table>'
echo " </body>" >> $html
echo "</html>" >> $html
done
If you can, please consider awk:
awk -F' : ' '
BEGIN{
print "<html><style type=\"text/css\">table, th, td {border: 1px solid black;}</style><body><table><th>HEADING1</th><th>HEADING2</th><th>HEADING3</th>"
}
{
print "<tr>"
for(i=1;i<=NF;i++)
print "<td>" $i "</td>"
print "</tr>"
}
END{
print "</table></body></html>"
}
' input_file > output.html
The BEGIN and END statement fills the static html tags.
The middle statement fills one line of the array according to each input file line by adding <tr> for each line and <td> for each field.

Set HTML table cell background colour according to text content

I have written a Perl program to create a web page with an HTML table derived from text file textfile.txt.
I would like to change it so that cells of the table are coloured according to the text content. For instance, if the text is Reject then background of the cell should be red.
Here are two methods that I tried. Neither of them worked
Method 1
if ( $_ eq "REJECT" ) {
print map { "<td style=width:705 bgcolor=#FF0000 >REJECT</td>" } #$d;
}
Method 2
foreach my $d ( #data ) {
$d //= ''; # Convert undefined values to empty strings
my $class;
if ( $d eq 'REJECT' ) {
$class = 'hilite';
}
$html .= '<td';
$html .= " class='$class'" if $class;
$html .= ">$d</td>";
}
Perl program
#!/usr/bin/perl
print "Content-type: text/html\n\n";
use strict;
use warnings;
my $output = `cat textfile.txt`;
my #lines = split /\n/, $output;
my #data;
foreach my $line ( #lines ) {
chomp $line;
my #d = split /\s+/, $line;
push #data, \#d;
}
my $color1 = "black";
my $color2 = "darkgreen";
my $color3 = "black";
my $color4 = "red";
my $color5 = "lime";
my $num = 6;
my $title = "This is the heading";
my $fstyle = "Helvetica";
print "<body bgcolor = $color3>";
print "<font color = $color5 face = $fstyle size = $num>$title</font><br />";
foreach my $d ( #data ) {
print "<html>";
print "<body>";
print "<table style=table-layout= fixed width= 705 height=110 text = $color4 border = 2 bordercolor = $color1 bgcolor = $color2>";
print "<tr>";
print map {"<th style=width:705 >Column1</th>"}
print map {"<th style=width:705 >Column2</th>"}
print "</tr>";
print "<tr>";
print map {"<td style=width:705 >$_</td>"} #$d;
if ( $d eq 'REJECT' ) {
print map {"<td style=width:705 bgcolor=#FF0000 >Reject</td>"} #$d;
}
print "</tr>";
print "</table>";
print "</body>";
print "</html>";
}
Input text file:
Column1 Column2
Accept Reject
Accept Reject
Accept Reject
This line
print map { "<td style=width:705 bgcolor=#FF0000 >Reject</td>"
is adding the background color RED to the cell but it is not matching the condition Reject.
Output
Here are some of the errors in your Perl code
As I said, you are misusing map
You are creating a new HTML document for every element of #data. What do you expect the browser to do with multiple <html> elements? It can't display them all
You are expecting the string REJECT to match Reject
You are using a mixture of CSS style strings and element attributes. For instance
print "<table style=table-layout= fixed width= 705 height=110 text = $color4 border = 2 bordercolor = $color1 bgcolor = $color2>"
should be
print qq{<table
style="table-layout:fixed; width=705; height=110; text=$color4"
border=2
bordercolor="$color1"
bgcolor="$color2">\n}
because table-layout, width, height, and text are CSS properties, while border, bordercolor, and bgcolor are HTML element attributes
I think you should be writing CSS to solve this problem, but that is another matter
It would also help you a lot if you printed a newline "\n" after each HTML element. That way the output will be much more readable and you will be able to see better what you have created
Please don't persist with this "try things until it works" approach. You always end up coming here for help to get you out of the mess you've created, and you're not asking intelligent questions. To be using map like that after so long means you're not learning at all, and you owe it to yourself as well as your employer to learn the language properly
Here is a solution that performs correctly, but it is no more than a correction of your own code. The problems that I have outlined have been fixed, and that is all
#!/usr/bin/perl
use strict;
use warnings 'all';
my $color1 = 'black';
my $color2 = 'darkgreen';
my $color3 = 'black';
my $color4 = 'red';
my $color5 = 'lime';
my $size = 6;
my $title = 'This is the heading';
my $fstyle = 'Helvetica';
print "Content-type: text/html\n\n";
print "<body bgcolor = $color3>\n";
print "<font color = $color5 face=$fstyle size=$size>$title</font><br />\n";
{
print "<html>\n";
print "<body>\n";
print qq{<table
style="table-layout:fixed; width=705; height=110; text=$color4"
border=2
bordercolor="$color1"
bgcolor="$color2">\n};
print "<tr>\n";
print qq{<th style="width:705" >Column1</th>};
print qq{<th style="width:705" >Column2</th>};
print "</tr>\n";
open my $fh, '<', 'textfile.txt' or die $!;
while ( <$fh> ) {
my #line = split;
print "<tr>\n";
for ( #line ) {
if ( /reject/i ) {
print qq{<td style=width:705 bgcolor=red>$_</td>};
}
else {
print "<td style=width:705>$_</td>"
}
}
print "</tr>\n";
}
print "</table>\n";
print "</body>\n";
print "</html>\n";
}
output
Content-type: text/html
<body bgcolor = black>
<font color = lime face=Helvetica size=6>This is the heading</font><br />
<html>
<body>
<table
style="table-layout:fixed; width=705; height=110; text=red"
border=2
bordercolor="black"
bgcolor="darkgreen">
<tr>
<th style="width:705" >Column1</th><th style="width:705" >Column2</th></tr>
<tr>
<td style=width:705>Column1</td><td style=width:705>Column2</td></tr>
<tr>
<td style=width:705>Accept</td><td style=width:705 bgcolor=red>Reject</td></tr>
<tr>
<td style=width:705>Accept</td><td style=width:705 bgcolor=red>Reject</td></tr>
<tr>
<td style=width:705>Accept</td><td style=width:705 bgcolor=red>Reject</td></tr>
</table>
</body>
</html>
Appearance
I still have misgivings about your approach. Hacking together a program from bits and pieces of others' work that you don't understand is a recipe for failure. If you have no inclination to explore and learn the details enough to survive on your own then you have chosen the wrong job
I think you should be using a template system such as
Template::Toolkit instead of printing HTML from your Perl program
The colours should be changed using CSS and an appropriate class, rather than printing HTML attributes in line
You seem to think that a casual and approximate approach is fine, or at least that you are unwilling to offer any more, but while that may be true of other professions, software engineering requires much more care and precision

Read txt file into HTML table

I have to read a txt file into a HTML table.
There are many fields in it but I only want to read the field that says "value".
Here is my txt file:
one=availability:, timestamp=90754, value=no
two=description:, timestamp=074693, value=not sure
three=Name, timestamp=90761, value=yes
The one, two, three values are my row headers and I want it to display the values underneath.
Is there anyway of doing this using iframe?? PHP is not working for me.
Supposing the value is always the last field and you are reading the file line by line I would use a dirty approach:
$value = explode('value=', $line)[1];
longwinded approach
$headers=[];
$values=[];
$lines = file('./homework.txt', FILE_IGNORE_NEW_LINES);
foreach($lines as $line){
$chunks = explode(',',$line);
foreach($chunks as $index => $chunk){
list($key, $value) = explode('=', trim($chunk));
if(!$index){
$headers[] = $value;
}
if('value' === $key){
$values[] = $value;
}
}
}
echo "<table><thead><tr>";
foreach($headers as $h){
echo "<th>{$h}</th>";
}
echo "</tr></thead><tbody><tr>";
foreach($values as $v){
echo "<td>{$v}</td>";
}
echo "</tr></tbody></table>";
If all rows follow the same pattern you can probably use:
//$textfilestring = "one=availability:, timestamp=90754, value=no
//two=description:, timestamp=074693, value=not sure
//three=Name, timestamp=90761, value=yes";
$textfilestring = file_get_contents("PATH_TO_FILE");
$arraylines = explode("\n", $textfilestring);
for ($i=0;$i<count($arraylines);$i++) {
$arraylines[$i] = explode("value=", $arraylines[$i]);
}
echo "<pre>";
var_dump($arraylines);
echo "</pre>";
echo $arraylines[0][1] . "<br>";
echo $arraylines[1][1] . "<br>";
echo $arraylines[2][1] . "<br>";
$arraylines should be twodimensional with one part beeing
one=availability:, timestamp=90754,
and one beeing
no
Not tested though.

HTML::TagFilter returning HTML::Element HASH object

New to Perl and I am digging what I can do as well as the support and documentation available for all these great libraries; however, I am having an issue with a script I am working on. Prior to implementing HTML::TagFilter, I was using line 63 (print FH $tree->as_HTML) to print to file the html content I was looking for. I looked specifically for everything in the body tag. Now I'd like to only print out the p tags, h tags, and img tags without any attributes. When I run my code, the files are created in the proper directory but in each file a hash object is printed (HTML::Element=HASH(0x3a104c8)).
use open qw(:locale);
use strict;
use warnings qw(all);
use HTML::TreeBuilder 5 -weak; # Ensure weak references in use
use URI::Split qw/ uri_split uri_join /;
use HTML::TagFilter;
my #links;
open(FH, "<", "index/site-index.txt")
or die "Failed to open file: $!\n";
while(<FH>) {
chomp;
push #links, $_;
}
close FH;
my $dir = "";
while($dir eq ""){
print "What is the name of the site we are working on? ";
$dir = <STDIN>;
chomp $dir;
}
#make directory to store files
mkdir($dir);
my $entities = "";
my $indent_char = "\t";
my $filter = HTML::TagFilter->new(
allow=>{ p => { none => [] }, h1 => { none => [] }, h2 => { none => [] }, h3 => { none => [] }, h4 => { none => [] }, h5 => { none => [] }, h6 => { none => [] }, img => { none => [] }, },
log_rejects => 1,
strip_comments => 1
);
foreach my $url (#links){
#print $url;
my ($filename) = $url =~ m#([^/]+)$#;
#print $filename;
$filename =~ tr/=/_/;
$filename =~ tr/?/_/;
#print "\n";
my $currentfile = $dir . '/' . $filename . '.html';
print "Preparing " . $currentfile . "\n" . "\n";
open (FH, '>', $currentfile)
or die "Failed to open file: $!\n";
my $tree = HTML::TreeBuilder->new_from_url($url);
$tree->parse($url);
$tree = $tree->look_down('_tag', 'body');
if($tree){
$tree->dump; # a method we inherit from HTML::Element
print FH $filter->filter($tree);
#print FH $tree->as_HTML($entities, $indent_char), "\n";
} else{
warn "No body tag found";
}
print "File " . $currentfile . " completed.\n" . "\n";
close FH;
}
Why is this happening and how can I print the actual content I am looking for?
Thank you.
$filter->filter() expects HTML, HTML::TreeBuilder is not HTML, but a subclass of HTML::Element. look_down() returns a HTML::Element. That is what you see from your print, because when you treat this reference as a string, you will get the string representation of the object. HTML::Element=HASH(0x7f81509ab6d8), which means that the object HTML::Element, which is solved by a HASH structure and the memory address of this object.
You can fix it all by calling filter with the HTML from the look_down:
print FH $filter->filter($tree->as_HTML);

post value in perl cgi and HTML not inserting properly

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
"
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/;
}