2017-02-22 18 views
0

私は.txtを.html形式に変換しようとしました。次のコードを変換しようとしましたが、perlを使ってテーブル形式の各列に異なる見出し名を付ける必要もあります。.txtファイルの内容を.html形式に変換するには?

入力ファイル:私は試み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 

コード:

#!/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 $!; 

期待出力:

カラム1はシリアル番号に応じてライン毎に全体の文章を印刷する必要があります副。

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 $!; 

は、次のHTMLテーブルを生成します:つまり、あなたのコードに最小限の変更で(明示的な配置詰まりがパッド領域内に作成されている)

|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 | 
+0

は何ですかあなたの予想される出力、およびあなたの受け取った出力?あなたは "各列の見出し名"と言いますが、列にはコードには何もありません。実際のエラーや警告が表示されている場合、メッセージは何ですか? – AntonH

+1

ようこそ!どのような人がいるかについては、[ツアー](https://stackoverflow.com/tour)と[how-to-ask](https://stackoverflow.com/help/how-to-ask/)のページをご覧ください探しています。この場合は、[あなたの質問を編集してください](https://stackoverflow.com/posts/42400948/edit)を入力して、実際に見ている出力と実際に見える出力を含めてください。 – cxw

+0

また、 'split'は配列を返します。あなたがそれをスカラーに使用する方法は、分割の要素ではなく結果の配列のサイズを返します。 – AntonH

答えて

4

から

<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> 
+0

これはうまく動作します@ jm666 –

関連する問題