我有以下腳本:
#!/usr/bin/perl
use warnings;
use strict;
my $count = 0;
my (@first , @second , @third);
while ($count <= 7){
push (@first , $count);
push (@second , $count) if defined $count;
push (@third , $count) if $count;
$count++;
}
print "first: @first\n";
print "second: @second\n";
print "third: @third\n";
這將產生以下的輸出:
first: 0 1 2 3 4 5 6 7
second: 0 1 2 3 4 5 6 7
third: 1 2 3 4 5 6 7
什麼把if defined $count
與if $count
,除了後者的方法不會零添加的區別數組?我搜索了perldocs,但找不到答案。
應該是:「當$ count評估!= 0」 – 2010-10-11 04:30:48
@nathan。乾杯 – 2010-10-11 08:17:01
實際上,他們都以相同的方式工作。這是他們測試的價值不同。第一個測試$ count的值,第二個是定義的$ count的值。如果()不關心你如何獲得價值。 – 2010-10-11 17:32:30