简体   繁体   中英

for loop with fetchall_arrayref adding to an array

How can I loop through my arrayref and push row data into an array I have?

Here is my code that does it once without a loop but what I want is it to do it for each row and not sure the best approach:

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

    my @excluded;

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

Or

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

If you don't need $data2 for anything else,

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

Or

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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