2008-10-20 38 views
1

對於HF50(HF $ HF),例如在「MyFile.txt」中,我是searching,因此提取的數據必須保存爲「save.txt」。 「save.txt」中的數據現在再次提取並填充參數並輸出到我的表格中。但是,當我嘗試代碼時,我沒有輸出,「save.txt」爲空。如何提取文本,保存並輸出到網頁?

Var $ HF無法識別我輸入的內容。請幫忙。

#! /usr/bin/perl 

print "Content-type:text/html\r\n\r\n"; 

use CGI qw(:standard); 
use strict; 
use warnings; 

my ($file,$line,$tester,$HF,$keyword); 
my ($f1,$f2,$f3,$f4,$f5,$f6,$f7,$f8,$f9,$f10,$f11,$f12,$f13,$f14,$f15,$f16,$f17,$f18,$f19); 

my $keyWord=param('keyword'); 
$HF=$keyWord; 

my $infile='MyFile.txt'; 
my $outfile='save.txt'; 

open (my $inhandle, '<',$infile) or die "Can't open $infile:$!"; 
open (my $outhandle, '>', $outfile) or die "Can't open $outfile:$!"; 


while (my $line=<$inhandle>){ 
if ($line=~ m/HF$HF/i) { 
print {$outhandle}$line; 
print $line; 

print "<HTML>"; 
print "<head>"; 
print "<body bgcolor='#4682B4'>"; 
print "<title>FUSION SHIFT REPORT</title>"; 
print "<div align='left'>"; 
print "<FORM METHOD='get'  ACTION='http://Shielex.com/pe/mrigos/mainhead.html'>"; 
print "<b>SEACRH:</b>"; 
print "<INPUT TYPE='text' NAME='rec' SIZE='12' MAXLENGHT='40'>"; 
print "<INPUT TYPE='submit' value='go'>"; 
print "</form>"; 
print "<TABLE CELLPADDING='1' CELLSPACING='1' BORDER='1' bordercolor=black width='100%'>"; 
print "<TR>"; 

print "<td width='11%'bgcolor='#00ff00'><font size='2'>TESTER No.</td>"; 
print "<td width='10%'bgcolor='#00ff00'><font size='2'>DATE</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 width='10%'bgcolor='#00ff00'><font size='2'>SMSLOT</td>"; 

print "<td width='12%'bgcolor='#00ff00'><font size='2'>LOADBOARD</td>"; 


print "<td width='10%'bgcolor='#00ff00'><font size='2'>CATEGORY</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 "</tr>"; 
print "<TR>"; 

$file='save.txt'; 
open(F,$file)||die("Could not open $file"); 
while ($line=<F>) 
{ 
my @cells=($f1,$f2,$f3,$f4,$f5,$f6,$f7,$f8,$f9,$f10,$f11,$f12,$f13,$f14,$f15,$f16,$f17,$f18,$f19)= split ',',$line; 

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'>$f5</TD>"; 
print "<TD bgcolor='#ADD8E6'><font size='2'>$f6</TD>"; 

print "<TD bgcolor='#ADD8E6'><font size='2'>$f8</TD>"; 

print "<TD bgcolor='#ADD8E6'><font size='2'>$f10</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>"; 

} 
} 
} 
close F; 
print "</TABLE>"; 
print "</body>"; 
print "<html>"; 


= MyFile.txt的數據 =
1,HF50,13-OCT-08,04:17:53,761503BZZGR-62,B2761503BP22.EVA,DWP,DWP,校準
2,HF60,13-OCT-08,04:17:53,761503BZZGR-62,B2761503BP22.EVA,DWP,DWP,校準
1,HF50,13-OCT-08,04:17:53, 761503BZZGR-62,B2761503BP22.EVA,DWP,DWP,校準

回答

0

你永遠不會關閉$outfile,所以它不會被刷新。但是,也許你想要將數據存儲在數組中?另外,您應該始終使用open()的三參數形式,並且在使用CGI程序時也應該始終使用絕對路徑,因爲在許多情況下,「當前目錄」不是您認爲的那樣。

+0

我再次代碼,並將其works..Thanks :) – Shiel 2008-10-21 13:33:10

1

您是否將此作爲CGI腳本運行?在這種情況下,您可能沒有權限打開文件進行寫入。您是否檢查了錯誤日誌,看看您的信息是否來自die

您可能想查看Troubleshooting Perl CGI scripts。完成所有步驟而不會跳過任何步驟。當你陷入困境時,你需要幫助我們幫助你的大部分信息。

祝你好運,:)

+0

謝謝,它幫助了我很多,, IM在我的程序的下一階段:) – Shiel 2008-10-21 13:41:27

-1

首先,perl的輸出本質上是緩衝的。因此,除非使用某種明確的方法,否則不能保證物理文件將有任何可讀的內容。正如有人提到,你必須以某種方式刷新輸出。我的意見如下代碼。 (你也可以通過關閉輸出文件並在追加模式後打開它來完成此操作。)

其次,它看起來並不像你想做的那樣看起來像你想要的去做。如果所有內容都完全刷新到文件中,則您要求輸入行的html標頭。所以當我在輸入中添加行時,它會打印出許多搜索框。我不指望那是你想要的。

這裏有一個更Perl的指明分數代碼:

use CGI qw(:standard); 
use IO::File; 
use strict; 
use warnings; 

my ($file,$line,$HF); #,$tester,$HF,$keyword); 
# don't pollute -> my ($f1,$f2,$f3,$f4,$f5,$f6,$f7,$f8,$f9,$f10 
# ,$f11,$f12,$f13,$f14,$f15,$f16,$f17,$f18,$f19); 


# my $keyWord=param('keyword'); <-- if you're not going to do anything with $keyWord 
$HF=param('keyword'); # <- assign it to the variable you're going to use 

my $infile='MyFile.txt'; 
my $outfile='save.txt'; 

open (my $inhandle, '<',$infile) or die "Can't open $infile:$!"; 
open (my $outhandle, '>', $outfile) or die "Can't open $outfile:$!"; 
# this would flush -> my $outhandle = IO::File->new(">$outfile"); 

print q{Content-type:text/html 

<HTML> 
<head> 
<title>FUSION SHIFT REPORT</title> 
<style type="text/css"> 
.header { background-color : #0f0; font-size : 12pt } 
.detail { background-color : #ADD8E6; font-size : 12pt } 
</style> 
</head> 
<body bgcolor='#4682B4'> 
<div align='left'> 
<FORM METHOD='get' ACTION='http://Shielex.com/pe/mrigos/mainhead.html'> 
<b>SEACRH:</b> 
<input type='text' name='rec' size='12' maxlenght='40'> 
<input type='submit' value='go'> 
</form> 
<table cellpadding='1' cellspacing='1' border='1' bordercolor=black width='100%'> 
<tr> 
    <td class="header" width='11%'>TESTER No.</td> 
    <td class="header" width='10%'>DATE</td> 
    <td class="header" width='11%'>DEVICE NAME</td> 
    <td class="header" >TEST PROGRAM</td> 
    <td class="header" width='10%'>SMSLOT</td> 
    <td class="header" width='12%'>LOADBOARD</td> 
    <td class="header" width='10%'>CATEGORY</td> 
    <td class="header" width='13%'>ROOT CAUSE 1</td> 
    <td class="header" width='13%'>ROOT CAUSE 2</td> 
</tr> 
}; 

my $hf_str = ",HF$HF,"; 
# OO -> $outhandle->autoflush(); <- set autoflush 
while (my $line=<$inhandle>){ 
    next unless index($line, $hf_str) > -1; 
    # OO -> $outhandle->print($line); 
    #  $outhandle->flush(); <- if autoflush not set, do it manually 
    print *{$outhandle} $line; 
    print "<tr>" 
     , (map { qq{<td class="detail">$_</td>} } 
      split ',', $line 
      ) 
     , "</tr>\n" 
     ; 
} 
print q{ 
</table> 
</body> 
</html> 
}; 
相關問題