shuttle folks
shuttle folks

Reputation: 47

How to convert .txt file contents into .html format using perl?

I tried to convert .txt into .html format.So i tried the following code to convert and also need to give different heading names for every column in table format using perl.

Input file:input.txt

1:Explicit Placement blockages are created in the pad regions ?:Yes:INCORRECT:To Be Done
2:Explicit Routing blockages are created in the pad regions ?:Yes:INCORRECT:To Be Done
3:Is Complete bond pad meal is used for top level power hookup ?:Yes:INCORRECT:To Be Done

code which i tried:

#!/usr/bin/perl
use strict;
use warnings;
open my $HTML, '>', 'output.html' or die $!;
print $HTML <<'_END_HEADER_';
<html>
<head><title></title></head>
<body>
_END_HEADER_
open my $IN, '<', 'input.txt' or die $!;
while (my $line = <$IN>) {
    $convert=split(/\:/,$line);
    print $HTML $convert;
}
print $HTML '</body></html>';
close $HTML or die $!;

Expected output:

Column 1 should print the entire sentence for every line according to serial number vice. i.e from (Explicit Placement blockages are created in the pad regions)

|s.no|column1      |column2|column3  |column4    |
|1   |Explicit.... |yes    |INCORRECT|To be done |
|2   |Explicit.... |yes    |INCORRECT|To be done |
|1   |Is.......... |yes    |INCORRECT|To be done |

Upvotes: 0

Views: 521

Answers (1)

clt60
clt60

Reputation: 63892

With minimal changes to your code:

use strict;
use warnings;

open my $HTML, '>', 'output.html' or die $!;
print $HTML <<'_END_HEADER_';
<html>
<head><title></title></head>
<body>
<table>
_END_HEADER_

open my $IN, '<', 'input.txt' or die $!;
while (my $line = <$IN>) {
    chomp $line;
    print $HTML '<tr><td>' . join('</td><td>', split(/:/,$line)) . "</td></tr>\n";
    #or
    #print $HTML '<tr><td>' . $line =~ s|:|</td><td>|gr . "</td></tr>\n";
}
close $IN or die $!;
print $HTML <<'_END_FOOTER_';
</table>
</body>
</html>
_END_FOOTER_
close $HTML or die $!;

will produce the following html-table:

<html>
<head><title></title></head>
<body>
<table>
<tr><td>1</td><td>Explicit Placement blockages are created in the pad regions ?</td><td>Yes</td><td>INCORRECT</td><td>To Be Done</td></tr>
<tr><td>2</td><td>Explicit Routing blockages are created in the pad regions ?</td><td>Yes</td><td>INCORRECT</td><td>To Be Done</td></tr>
<tr><td>3</td><td>Is Complete bond pad meal is used for top level power hookup ?</td><td>Yes</td><td>INCORRECT</td><td>To Be Done</td></tr>
</table>
</body>
</html>

Upvotes: 4

Related Questions