Ilan Kleiman
Ilan Kleiman

Reputation: 1209

How to prematurely detect whether it's the last iteration during a while loop in Perl

Similar question(does not solve my question): Is it possible to detect if the current while loop iteration is the last in perl?

Post above has an answer which solves the issue of detecting whether it's the last iteration solely when reading from a file.

In a while loop, is it possible to detect if the current iteration is the last one from a mysql query?

while( my($id, $name, $email) = $sth->fetchrow_array() ) 
{
       if(this_is_last_iteration)
       {
            print "last iteration";
       }
}

Upvotes: 0

Views: 1719

Answers (4)

Dave Sherohman
Dave Sherohman

Reputation: 46187

Although you can count rows to tell when you are on the last result from an SQL query, no, in the general case it is not possible to know in advance whether you're on the last iteration of a while loop.

Consider the following example:

while (rand() > 0.05) {
  say "Is this the last iteration?";
} 

There is no way to predict in advance what rand() will return, thus the code within the loop has no way of knowing whether it will iterate again until the next iteration starts.

Upvotes: 1

dave
dave

Reputation: 11975

You'll need to verify this compiles, but a rough outline is:

my($rows) = $sth->rows;
my($i) = 0;
while( my($id, $name, $email) = $sth->fetchrow_array() ) 
{
    $i++;
    if ($i == $rows)
    {
        print "last iteration";
    }
}

If you give us some more context, there may be other options. For example, your print statement is the last thing in the while loop. If that matches reality, you could simply move this after the loop and do away with the counter.

A couple of commentators have correctly noted that the rows command will not always have the correct value for a SELECT command (eg. see here). If you're using SELECT (which seems likely from your code) then this could be an issue. You could perform a COUNT before the SELECT to get the number of rows provided the data set does not change between the COUNT and the SELECT.

Upvotes: 2

ikegami
ikegami

Reputation: 385655

my $next_row = $sth->fetch();
while (my $row = $next_row) {
    my ($id, $name, $email) = @$row;

    $next_row = $sth->fetch();
    if (!$next_row) {
        print "last iteration";
    }

    ...
}

Upvotes: 6

shieldgenerator7
shieldgenerator7

Reputation: 1736

You can keep a counter and compare it to the array length. I'm not familiar with Perl, but that's how I would do it in any other language.

Upvotes: -3

Related Questions