繁体   English   中英

DBI :: fetchall_arrayref的Perl数组取消引用问题

[英]Perl Array Dereference Problem with DBI::fetchall_arrayref

我是Perl新手,在解引用数组时遇到问题,该数组是DBI模块中fetchall_arrayref的结果:

my $sql = "SELECT DISTINCT home_room FROM $classlist";
my $sth = $dbh->prepare($sql);
$sth->execute;
my $teachers = $sth->fetchall_arrayref;
foreach my $teacher (@{$teachers}) {
    print $teacher;
}

运行此命令将打印引用而不是数组中的值。

但是,当我运行时:

my $arrref = [1,2,4,5];
foreach (@{$arrref}) {
print "$_\n";
}

我得到数组的值。

我究竟做错了什么? 谢谢您的帮助!

杰夫

文档

fetchall_arrayref方法可用于从准备和执行的语句句柄中获取所有要返回的数据。 它返回对数组的引用,该数组每行包含一个引用

因此,在您的示例中, $teacher是ARRAY引用。 因此,您将需要遍历此数组引用

foreach my $teacher (@{$teachers}) {
    foreach my $titem (@$teacher) {
        print $titem;
    }
}

如果您只想提取教师列,则可以使用:

my @teachers = @{$dbh->selectcol_arrayref($sql)};

fetchall_arrayref获取查询的所有结果,因此您实际上要返回的是对数组数组的引用。 返回的每一行将是各列的arrayref。 由于查询只有一列,因此您可以说:

my $teachers = $sth->fetchall_arrayref;
foreach my $teacher (@{$teachers}) {
    print $teacher->[0];
}

得到你想要的。

查看更多:

Perl中的数组数组

您有对行数组的引用。 每行都是对字段数组的引用。

foreach my $teacher_row (@$teachers) {
    my ($home_room) = @$teacher_row;
    print $home_room;
}

您会看到Data :: Dumper的区别。

use Data::Dumper;
print(Dumper($teachers));
print(Dumper($arrref));

$sth->fetchall_arrayref返回对数组的引用,该数组每行包含一个引用!
这里看看DBI文档。

根据DBI的fetchall_arrayref()文档:

fetchall_arrayref方法可用于从准备和执行的语句句柄中获取所有要返回的数据。 它返回对数组的引用,该数组每行包含一个引用。

您是一级间接访问:

my $sql = "SELECT DISTINCT home_room FROM $classlist";
my $sth = $dbh->prepare($sql);
$sth->execute;
my $teachers = $sth->fetchall_arrayref;
foreach my $teacher (@{$teachers}) {
    local $" = ', ';
    print "@{$teacher}\n";
}

有时,数据结构可能难以可视化。 发生这种情况时,我求助于Data::Dumper以便可以插入如下代码:

print Dumper $teacher;

我发现有时通过转储数据结构,我可以在创建用于操纵该结构的代码时获得一个即时映射用作参考点。 最近,我不时地使用Dumper来理清头绪,经历了一个结构的真实噩梦。

您可以使用map取消引用返回的结构:

@teachers = map { @$_->[0] } @$teachers;

现在您有了一个简单的教师队伍。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM