好吧,上次由于交代不是很清楚,搞了个抽样放回的程序,今天重新弄了一下,把它写成模块的形式:
package Sampling; use warnings; use strict; require Exporter; our @ISA = qw(Exporter); our @EXPORT_OK = qw(swor fac); #swor -- sampling without replacement sub swor{ my ($sample_num,$sampling_num) = @_; my @array = (1..$sample_num); my @percom; for(my $i = 0; $i < $sampling_num; $i ++) { my $index = int(rand(@array)); push @percom,$array[$index]; my @tmp; if($index eq 0) { @tmp = @array[($index + 1)..$#array]; }elsif($index eq $#array){ @tmp = @array[0..($index - 1)]; }else{ @tmp = (@array[0..($index - 1)],@array[($index + 1)..$#array]); } @array = (); @array = @tmp; } return @percom; } sub fac{ my $value = shift; if($value eq 1) { return 1; }elsif($value eq 0){ return 1; }else{ return $value * &fac($value - 1); } }