Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

while (scalar(@names)) { print "?"; my $sql=$base_sql.($#names?'in ('.join(',',map('?',@names)).')':'=?' +); my $sth=$dbh->prepare($sql); my $rv=$sth->execute(@names); my ($res,$status) = $sth->fetchall_arrayref(); @names=map($res->[$_][0],0..$#$res); push @ref, join '|',@names if (scalar(@names)); }
Thats the code that keeps on going. I want it to stop when it reaches $ref2, but I cnat do it. Mine just keeps on looping, What should I do
while (scalar(@names)) { $b = @ref; if($b < 2) { print "?"; my $sql=$base_sql.($#names?'in ('.join(',',map('?',@names)).')':'=?' +); my $sth=$dbh->prepare($sql); my $rv=$sth->execute(@names); my ($res,$status) = $sth->fetchall_arrayref(); @names=map($res->[$_][0],0..$#$res); push @ref, join '|',@names if (scalar(@names)); } }

Replies are listed 'Best First'.
Re: Looper
by DamnDirtyApe (Curate) on Nov 24, 2002 at 03:27 UTC

    How about adding

    last if $#ref == 3 ;

    As the last line of the loop? (untested)


    _______________
    DamnDirtyApe
    Those who know that they are profound strive for clarity. Those who
    would like to seem profound to the crowd strive for obscurity.
                --Friedrich Nietzsche
      that worked but I also want to do this, i want my results to not just be the username, but username=$status, status is andother column in my database.
Re: Looper
by Enlil (Parson) on Nov 24, 2002 at 03:30 UTC
    I am assuming $ref[2] is what is meant by, if
    $b = @ref; if ($b < 2) { ... }
    If you mean to stop it when $b == 3 (as that is when $ref[2] exists). Why not just stop looping at this point using last. Like so:
    last if $b == 3;
    As soon as this is evaluates as true, it will break out of the loop, which in this case is the while loop. If this is not what you wanted could you be a little more descriptive as to what it is that you want to accomplish (I am mainly guessing with my response).

    -enlil

      Why not ditch the intermediate and cut to the chase:
      last if (@ref > 2);  # More than two elements
      Update: Make that @ref instead of @b