可能的方式:有條件地在列表中包含元素的最佳方式是什麼?
使用push:
my @list; push @list, 'foo' if $foo; push @list, 'bar' if $bar;
-
my @list = ( $foo ? 'foo' :(), $bar ? 'bar' :(), );
使用
x!!
Boolean list squash operator:my @list = ( ('foo') x!! $foo, ('bar') x!! $bar, );
哪一個更好?爲什麼?
只是一個側面說明,你不使用任何這些字符串變量引用的,是嗎?沒有'$ {$ list [0]}'?這只是看起來像一個哈希給我。 – 2010-02-19 12:12:24
投票問題類型問題應該是CW。 – 2010-02-19 12:16:06
@Chris:不,我只是爲了簡單而使用了字符串。 – 2010-02-19 12:29:58