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

发布于 2024-11-16 20:35:08 字数 484 浏览 3 评论 0原文

我是 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";
}

我得到了数组的值。

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

杰夫

I'm a Perl newbie and am having issues with dereferencing an array that is a result of fetchall_arrayref in the DBI module:

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;
}

Running this will print the reference instead of the values in the array.

However, when I run:

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

I get the values of the array.

What am I doing wrong? Thank you for your help!

Jeff

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

缱倦旧时光 2024-11-23 20:35:08

来自文档

fetchall_arrayref 方法可以是
用于获取所有数据
从准备和执行返回
语句句柄。 它返回一个
对包含以下内容的数组的引用
每行一个引用

因此,在您的示例中, $teacher 是一个 ARRAY 引用。
所以你需要循环遍历这个数组引用

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

From the doc

The fetchall_arrayref method can be
used to fetch all the data to be
returned from a prepared and executed
statement handle. It returns a
reference to an array that contains
one reference per row
.

So in your example, $teacher is an ARRAY ref.
So you will need to loop through this array ref

foreach my $teacher (@{$teachers}) {
    foreach my $titem (@$teacher) {
        print $titem;
    }
}
烂柯人 2024-11-23 20:35:08

如果您只想提取教师列,则需要使用:

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

if you want to extract only the teacher column, you want to use:

my @teachers = @{$dbh->selectcol_arrayref($sql)};
┾廆蒐ゝ 2024-11-23 20:35:08

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

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

得到您想要的。

查看更多内容:

Perl 中的数组的数组

fetchall_arrayref fetches all the results of the query, so what you're actually getting back is a reference to an array of arrays. Each row returned will be an arrayref of the columns. Since your query has only one column, you can say:

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

to get what you want.

See more:

Arrays of arrays in Perl.

油焖大侠 2024-11-23 20:35:08

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

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

您会看到 Data::Dumper 的差异。

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

You have a reference to an array of rows. Each row is a reference to an array of fields.

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

You would have seen the difference with Data::Dumper.

use Data::Dumper;
print(Dumper($teachers));
print(Dumper($arrref));
我为君王 2024-11-23 20:35:08

$sth->fetchall_arrayref 返回对每行包含一个引用的数组的引用!
请在此处查看 DBI 文档。

$sth->fetchall_arrayref returns a reference to an array that contains one reference per row!
Take a look at DBI docs here.

穿越时光隧道 2024-11-23 20:35:08

根据 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 来理顺我的头。

Per the documentation of DBI's fetchall_arrayref():

The fetchall_arrayref method can be
used to fetch all the data to be
returned from a prepared and executed
statement handle. It returns a
reference to an array that contains
one reference per row.

You're one level of indirection away:

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";
}

The data structure might be a little hard to visualize sometimes. When that happens I resort to Data::Dumper so that I can insert lines like this:

print Dumper $teacher;

I've found that sometimes by dumping the datastructure I get an instant map to use as a reference-point when creating code to manipulate the structure. I recently worked through a real nightmare of a structure just by using Dumper once in awhile to straighten my head out.

最后的乘客 2024-11-23 20:35:08

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

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

现在您有一个简单的教师数组。

You can use map to dereference the returned structure:

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

Now you have a simple array of teachers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文