Arrays 如何在perl中一次创建多个数组
我正在尝试创建23个数组,但不键入@array1、@array2等等,如果$chrid与数组号匹配,则使用数组@r中的变量加载每个数组,如果$chrid=1,则应将其放置在@array1中。我怎样才能做到这一点 以下是我到目前为止的情况:Arrays 如何在perl中一次创建多个数组,arrays,perl,Arrays,Perl,我正在尝试创建23个数组,但不键入@array1、@array2等等,如果$chrid与数组号匹配,则使用数组@r中的变量加载每个数组,如果$chrid=1,则应将其放置在@array1中。我怎样才能做到这一点 以下是我到目前为止的情况: #!/usr/bin/perl use warnings; use strict; my @chr; my $input; open ($input, "$ARGV[0]") || die; while (<$input>) { my @
#!/usr/bin/perl
use warnings;
use strict;
my @chr;
my $input;
open ($input, "$ARGV[0]") || die;
while (<$input>) {
my @r = split(/\t/);
my $snps = $r[0];
my $pval = $r[1];
my $pmid = $r[2];
my $chrpos = $r[3];
my $chrid = $r[4];
for ($chrid) {
push (@chr, $chrid);
}
}
close $input;
您可以使用数组数组,其中每个子数组存储在数组数组中按顺序递增的索引中。下面是它的外观,但我仍然不清楚您想要存储什么样的数据:
use warnings;
use strict;
my @chr;
open my $input_fh, '<', $ARGV[0]
or die "Unable to open $ARGV[0] for reading: $!";
while (< $input_fh> ) {
# you can unpack your data in a single statement
my ($snps, $pval, $pmid, $chrpos, $chrid) = split /\t/;
# unclear what you actually want to store
push @{ $chr[$chrid] }, ( $snps, $pval, $pmid, $chrpos, $chrid );
}
close $input_fh;
使用二维数组。。请参阅引用SinanÜnür的话,当您发现自己在变量名中添加了一个整数后缀时,请考虑“我应该使用一个数组”。在本例中,您需要一个数组数组,正如Håkon Hægland所说。您是否建议我创建一个具有23维的数组?@Jack No,一个具有23个元素的数组,其中每个元素都是对另一个数组的引用。很抱歉,我向Håkon HæglandThanks询问了这个问题,我想存储一行,其中包含$snps、$pval、$pmid、$chrpos和$chrid