# get the top 3 candidates from the db while (my $wref = $sth->fetchall_arrayref([0], 3)){ my $w = $wref->[0]; foreach my $r (@running){ unless ($r eq $w){ # put $w to work } else{ # try the next $w } } }