1
我想在我的Perl代碼中做數組的總和,但我無法獲得正確的輸出。Perl數組覆蓋
這裏是我的示例代碼
use File::Find::Rule;
use Date::Parse;
my ($dir, $type, $fh, $line, $str_1,
$str_2,$str_3, $str_4);
my @array;
$dir = '/dir/test/';
$type = '*';
$str_1 = 'somestr1';
$str_2 = 'somestr2';
$str_3 = 'somestr3';
$str_4 = 'somestr4';
my @files = File::Find::Rule->file()->name($type)->in($dir);
open $out, '>>', "output_log" or die "Unable to open 'output_log' : $!";
print $out "\Logs \n";
print $out "--------------------------\n";
close $out or die "Unable to finish writing output_log : $!";
for my $file (@files) {
open $fh, '<', $file or die "can't open $file: $!";
open $out, '>>', "output_log" or die "Unable to open 'output_log' : $!";
while ($line = <$fh>) {
if ($line !~ /$str_1/ && $line =~ /$str_2/)
{
@array = $somevar # result of this loop 2,3
}
if ($line !~ /$str_3/ && $line =~ /$str_4/)
{
@array = $somevar #result of this loop 2,3,4,5,6
}
}
close $out or die "Unable to finish writing output_log : $!";
}
所以這是我想
@array = (2,3,2,3,4,5,6)
and sum of @array
但unfornately,如果我打印此陣列運行每一條線路,而是我想寫一個從兩個if塊存儲數組結果的循環.. 現在代碼覆蓋第二個if塊中的@array。希望我明確這一點!請幫忙
我希望你在程序的頂部有'使用嚴格'和'使用警告'全部'?它們在您編寫的每個Perl程序中都很重要。而且你不能在文件的頂部聲明所有的變量,因爲它使得簡單的錯誤更難找到。過去這對於很老的C編譯器來說是必需的,但現在我不能想到任何語言,在哪裏最好定義每個變量都不是最好的。 – Borodin