2012-06-02 50 views
0

我需要經過一定的數字並找到某些字符串中的數字,然後根據該數字我需要將其輸出到某個文件。基本上,我的問題是如何打開一個文件句柄根據循環迭代在循環中輸出?根據循環中的迭代輸出的文件句柄

示例代碼:

$rec_1= "481"; 

for my $all (@seq) 
{ 
    my $match = index($rec_1, $seq[$all]); 
    if ($match != -1) 
    { 
    # I want to open a file handle and output the contents of rec_1 
    # accordingly. (there will be 12 different files in the end.) 
    } 
    else 
    { 
    # print the data from rec_1 to not matches (another file) 
    } 
} 

在總,我知道會有,我將不得不尋找,雖然,所以我需要檢查,如果序列是有我只是經過rec_1數據,如果12點的序列rec_1包含該序列,我將它添加到具有以前數據的文件中。

+0

你如何獲得12個不同的文件嗎?給我們一個你想要展示的組合輸入樣本。 –

回答

1

您可以使用文件句柄的一個這樣的數組:

#!/usr/bin/perl 
use warnings; 
use FileHandle; 

my @fh; 
$fh[0] = FileHandle->new; 
$fh[0]->open(">file0") or die "open failed"; 
my $i = 0; 
$fh[$i]->print("Output for file 0"); 
+0

在FileHandle上優先使用[IO :: File](http://p3rl.org/IO::File)來刪除一個無用的間接層。 – daxim