for fet循环,fetchall_arrayref添加到数组

时间:2013-12-17 17:04:20

标签: perl

如何遍历我的arrayref并将行数据推送到我拥有的数组中?

这是我的代码在没有循环的情况下执行一次,但我想要的是为每行执行此操作并且不确定最佳方法:

 my $data2 = $sth->fetchall_arrayref({});
 my $excludeFirstName = $data2 ->[0]->{EXCL_FRSTNAME};
 my $excludeLastName = $data2 ->[0]->{EXCL_LASTNAME};

    my @excluded;

    push (@excluded, $excludeFirstName." ".$excludeLastName);

1 个答案:

答案 0 :(得分:3)

my @excluded;
for my $row (@$data2) {
   push @excluded, "$row->{EXCL_FRSTNAME} $row->{EXCL_LASTNAME}";
}

或者

my @excluded = map { "$row->{EXCL_FRSTNAME} $row->{EXCL_LASTNAME}" } @$data2;

如果您不需要$data2

my @excluded;
while (my $row = $sth->fetch_hashref()) {
   push @excluded, "$row->{EXCL_FRSTNAME} $row->{EXCL_LASTNAME}";
}

或者

my $sql = 'SELECT CONCAT(EXCL_FRSTNAME, EXCL_LASTNAME) FROM ...';
my $excluded = $dbh->selectcol_arrayref($sql);