需要使用随机排列数字的循环的帮助,并将排列后的值提供给perl代码



我有一个写得很好的perl代码(不是我写的(,我想用它进行一些分析,并需要它用不同的输入参数集迭代1000多次。我将解释:

perl cmh-test.pl --input C1_E1_C2_E2_C3_E3_C4_E4_java.sync --output C1_E1_C2_E2_C3_E3_C4_E4_java.latest.cmh --min-count 20 --min-coverage 30 --max-coverage 400 --population 1-2,3-4,5-6,7-8 --remove-temp

我现在想运行相同的代码1000次,但每次都要更改--population参数,比如第一次它是1-2,3-4,5-6,7-8,下次它变成1到8的随机排列,比如1-3,2-4,5-7,6-8等等。我该怎么做?

任何帮助都将不胜感激。

正如brian d foy在评论中所建议的那样,请参阅daxim发布在Reddit上的答案:https://www.reddit.com/r/perl/comments/jxneae/need_help_for_a_loop_with_random_permuted_numbers/

以下是不使用Algorithm::Combinatorics的替代解决方案。注意,shuffle可以生成重复排列。还要注意,每次运行脚本时,使用如下所示的常量随机种子会生成相同的排列序列。如果您不关心脚本在实例之间的可重复性,请记住删除srand语句。

#!/usr/bin/env perl
use strict;
use warnings;
use feature qw( say );
use List::Util qw( shuffle );
# Use fixed seed to generate reproducible results across calls to this
# script. Omit this to use a default seed every instance:
srand 42; 
my @orig_pop = 1..8;
# Using strings to name output files: *_0001.cmh .. *_1000.cmh.
# Use numbers (1 .. 1000) if you want output files named: *_1.cmh .. *_1000.cmh.
for my $iter ( '0001' .. '1000' ) {
my @pop = shuffle @orig_pop;
# Using a hash to make array @pop into pairs:
my %pop = @pop;
my $pop = join ',', map { "$_-$pop{$_}" } grep { exists $pop{$_} } @pop;
my $cmd = qq{perl cmh-test.pl } .
qq{--input C1_E1_C2_E2_C3_E3_C4_E4_java.sync } .
qq{--output C1_E1_C2_E2_C3_E3_C4_E4_java.latest_${iter}.cmh } .
qq{--min-count 20 --min-coverage 30 --max-coverage 400 } .
qq{--population $pop --remove-temp};
say $cmd;
}

最新更新