我假設「非常大的基因組」被存儲在一個非常大的文件,而且它是罰款的行數來收集數據(而不是由字符數),因爲這是第一個提到的標準。
然後,您可以逐行讀取文件並組裝行,直到其中有135行。然後它們交給一個程序或例行程序處理的是,清空緩衝區,並繼續前進
use warnings;
use strict;
use feature 'say';
my $file = shift || 'default_filename.txt';
my $num_lines_to_process = 135;
open my $fh, '<', $file or die "Can't open $file: $!";
my ($line_counter, @buffer);
while (<$fh>) {
chomp;
if ($line_counter == $num_lines_to_process)
{
process_data(\@buffer);
@buffer =();
$line_counter = 0;
}
push @buffer, $_;
++$line_counter;
}
process_data(\@buffer) if @buffer; # last batch
sub process_data {
my ($rdata) = @_;
say for @$rdata; say '---'; # print data for a test
}
如果處理應用程序/例行想要一個字符串,每次都可以追加到一個字符串,而不是添加到數組,$buffer .= $_;
並根據需要通過$buffer = '';
清除。
如果您需要傳遞一個字符串,但在收集數據時也有一些數組的使用(中間檢查/修剪/處理?),然後將行收集到一個數組中並根據需要使用,然後加入到一個字符串中交給它,my $data = join '', @buffer;
您也可以使用$.
variable和modulo operator (%
)
while (<$fh>) {
chomp;
push @buffer, $_;
if ($. % $num_lines_to_process == 0) # every $num_lines_to_process
{
process_data(\@buffer);
@buffer =();
}
}
process_data(\@buffer) if @buffer; # last batch
在這種情況下,我們需要先儲存一個線,然後檢查其編號,因爲$.
(行號讀取一個文件句柄,參見上面鏈接的文檔)從1(不是0)開始。
歡迎使用stackoverflow。看看[我應該怎麼做當有人回答我的問題?](http://stackoverflow.com/help/someone-answers)。那裏還有很多其他簡短而有用的幫助頁面。 – zdim