下面是一段Perl代码。我想使用不同的正则表达式($myo)和不同的运算符($op)遍历几个查询,并将结果保存到一个数组数组中,而不是一个大型的@result数组中。
也就是说,MYO[0-9]*$的结果数组将是每个运算符$results[0][0]、$results[0][1]……的数组。对于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;
}
}发布于 2013-01-19 01:21:54
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;
}发布于 2013-01-19 01:10:12
使用fetchall_arrayref方法而不是fetchrow_array方法。
因此,只需替换这一行:
push @results, $sth->fetchrow_array;使用这一行:
push @results, $sth->fetchall_arrayref;Here is the documentation for all the DBI statement handler methods.
发布于 2013-01-19 01:17:47
您可以只使用数组引用:
my $ref;
for my $i (1..10) {
for my $j (1..10) {
push @{$ref->[$i]}, $j;
}
}这样就可以了。
编辑:这将创建一个对10x10矩阵的引用。
https://stackoverflow.com/questions/14403968
复制相似问题