Redian新闻
>
请看看这个Perl random sampling code 有什么问题,
avatar
请看看这个Perl random sampling code 有什么问题,# Programming - 葵花宝典
m*b
1
想从一个array中随机抽样,比如,取两个.应当是常见问题.
#!/usr/local/bin/perl
#Define an Array and sampling size
my @myArray=('a','c','d','e','f','g','h','j','k','m','n','t');
my $numPick=2;
#use sub:selection_sample()
my @randPick=selection_sample(\@myArray,$numPick);
print (@randPick);
#### subroutine from perlmonk ####
sub selection_sample {
my ($array,$num)[email protected]_;
die "Too few elements (".scalar(@$array).") to select $num from\n"
unless $nummy @results;
my $pos=0;
while ( @results
avatar
t*t
2
you returned a ref to array, and assign it to array? dereference it first

【在 m***b 的大作中提到】
: 想从一个array中随机抽样,比如,取两个.应当是常见问题.
: #!/usr/local/bin/perl
: #Define an Array and sampling size
: my @myArray=('a','c','d','e','f','g','h','j','k','m','n','t');
: my $numPick=2;
: #use sub:selection_sample()
: my @randPick=selection_sample(\@myArray,$numPick);
: print (@randPick);
: #### subroutine from perlmonk ####
: sub selection_sample {

avatar
c*t
3
can you do $num < @$refArray?

【在 m***b 的大作中提到】
: 想从一个array中随机抽样,比如,取两个.应当是常见问题.
: #!/usr/local/bin/perl
: #Define an Array and sampling size
: my @myArray=('a','c','d','e','f','g','h','j','k','m','n','t');
: my $numPick=2;
: #use sub:selection_sample()
: my @randPick=selection_sample(\@myArray,$numPick);
: print (@randPick);
: #### subroutine from perlmonk ####
: sub selection_sample {

avatar
c*x
4
it should be like : print "@array\n";
print @array, would print the array's address, not the elements.
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。