perl 編寫execl讀寫小程序收獲——小小白
1.use strict 聲明
表明在使用變量前需事先聲明,即my $var或my @var,否則腳本運行會報錯;但網上都說,use strict 能幫忙發現錯誤,建議加上。
2.<>
鉆石操作符,是行輸入操作符的特例,是從用戶指定位置讀取,可指定輸入文件;
3.chomp
作用於variable 或 list,負責刪除標量型變量中的最後一個字符,或者數組中每個字的最後一個字符,並保證只有該行末字符是換行符時才進行刪除操作。
4.Spreadsheet::WriteExcel
主要需要使用該模塊中的format,使輸出的目標內容加上相應的格式,add_format();如果不需要特殊格式,print方式輸出至xls文件也很方便;
$worksheet->write_row($row, 0, \@list);或 $worksheet->write_row($row, 0, \@list,$format)每行以數組格式寫入,每個cell一個元素,特殊格式或無;
全部代碼如下:
#!/usr/bin/perl
use strict;
my $line;
my @line;
my @array;
my @word;
my $word;
my @list;
my $list;
my @array1;
my @array2;
my $col;
my $row;
my $format;
my $worksheet;
my $worksheet1;
my @array3;
my @array4;
my @line0;
while(defined ($line = <>)){
chomp $line;
if ($line =~ m/[p]/){
my @word = split/\s+/,$line;
push(@line,$line);
push(@array1,$word[5]);
push(@array2,$word[6]);
push(@array4,$word[1]);
}
}
$col = $row = 1;
use Spreadsheet::WriteExcel;
my $workbook = Spreadsheet::WriteExcel->new(‘D:\UltraEdit_v25.0.0.53_x64\perl.xls‘);
# Add a worksheet
$worksheet = $workbook->add_worksheet();
$format = $workbook->add_format();
$format->set_bold();
$format->set_bg_color(‘blue‘);
$format->set_align(‘center‘);
print $line[0]."\n";
my @line0 = split/\s+/,$line[0];
$worksheet->write_row(‘A1‘, \@line0);
sub which_element_is {
foreach (0..$#line){
if ($array1[$_] >= 0.01 and $array2[$_] >= 2000){
my @list = split/\s+/,$line[$_];
$worksheet->write_row($row, 0, \@list);
$row++;
}
elsif ($array1[$_] >= 0.005 and $array1[$_] < 0.01 and $array2[$_] >= 2000 and $array4[$_] eq "EGFR"){
my @list = split/\s+/,$line[$_];
$worksheet->write_row($row, 0, \@list, $format);
$row++;
}
}
}
which_element_is();
perl 編寫execl讀寫小程序收獲——小小白