Passing Array as a Parameter in PERL
在Perl中若想將傳入的陣列當作參數使用 必需經過特殊處理才能正常運作
By ET
There are more than two ways to do it. I just put down two quick ways.
- using reference to array
- not using reference to array
Method 1:
In program:
processarray(\@array);
In sub processarray:
my $arrayref=shift;
my @array=@$arrayref;
Method 2:
In program:
processarray(@array);
In sub processarray:
my @array=@_;
#進階:Perl將多個陣列各個當作參數使用的方法
#mutiArray.pl
@arr1 = (1 .. 10);
@arr2 = (11 .. 20);
@arr3 = (21 .. 30);
@arr = (\@arr1,\@arr2);
sub paraShow{
for (@_){
$array_ref=$_;
print "array_para:@$array_ref\n";
}
}
paraShow([@arr1,@arr2],[@arr3]);
#$_有兩個參數 (@arr1,@arr2)為第一個參數,而(@arr3)為第二個參數 共兩個參數
沒有留言:
張貼留言