[英]How do I use Perl to push into an array of arrays?
下面是Perl代碼的片段。 我想遍歷具有不同正則表達式( $myo
)和不同運算符( $op
)的幾個查詢,並將結果保存到數組數組而不是一個大@result
數組。
即, MYO[0-9]*$
$results[0][0]
數組將是每個運算符$results[0][0]
, $results[0][1]
...和MYO[0-9]*R$
的數組MYO[0-9]*R$
, $results[1][0]
, $results[1][1]
。
有任何想法嗎?
my @tech_ops = ("AR","DB","GM","LW","MM","SA");
my @results;
for my $myo (qw(MYO[0-9]*$ MYO[0-9]*R$ MYO[0-9]*T$ MYO[0-9]*U$)) {
foreach $op (@tech_ops)
{
$sth->execute($myo, $date_stop, $date_start,$op)
or die "Couldn't execute query for $myo: " . $sth->errstr;
push @results, $sth->fetchrow_array;
}
}
使用fetchall_arrayref
方法而不是fetchrow_array
方法。
因此,只需替換此行:
push @results, $sth->fetchrow_array;
用這行:
push @results, $sth->fetchall_arrayref;
my @tech_ops = ("AR","DB","GM","LW","MM","SA");
my @results;
for my $myo (qw(MYO[0-9]*$ MYO[0-9]*R$ MYO[0-9]*T$ MYO[0-9]*U$)) {
my @myo_results;
foreach $op (@tech_ops) {
$sth->execute($myo, $date_stop, $date_start,$op)
or die "Couldn't execute query for $myo: " . $sth->errstr;
push @myo_results, $sth->fetchrow_array;
}
push @results, \@myo_results;
}
您可以只使用數組引用:
my $ref;
for my $i (1..10) {
for my $j (1..10) {
push @{$ref->[$i]}, $j;
}
}
這樣就可以了。
編輯:這將創建對10x10矩陣的引用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.