in reply to Updating variable in a loop

If I understand you correctly, here's an untested example that should provide guidance:

my $count = 0; my @sent; while (get_entry_from_db()){ if (/match/){ ...; # send email and do other stuff $count++; push @sent, [$contact, $email]; } } print "users mailed: $count\n"; for my $entry (@sent){ my ($contact, $email) = @$entry; print "email sent to: $contact <$email><br>"; }

I've used an array of arrays here, instead of just appending the full string to the array, but you can go that route as well.

update: changed default var to named in for() loop for clarity

Replies are listed 'Best First'.
Re^2: Updating variable in a loop
by htmanning (Friar) on Jun 07, 2016 at 22:18 UTC
    So I would use another loop within my loop? Here's what I'm doing:
    $SQL = "Select * from database where (TO_DAYS(enddate) - TO_DAYS(Now() +) = 8) AND payment!='premium'"; &Do_SQL; $recordcount = $sth->rows; if ($recordcount > 0) { $sendEmails = 'yes'; } if ($sendEmails eq 'yes') { $count = 0; while ($pointer = $sth->fetchrow_hashref) { $contact = $pointer->{'contact'}; $toemail = $pointer->{'email'}; open (MAILPROG....."; SEND THE EMAIL HERE.... close (MAIL); print "email sent to: $contact <$toemail>\n"; $count++ } print "Sent $count emails.\n"; $action = "Email Reminders sent!"; $log = "Sent $count emails."; &log_it;
    I'd like $log to also have every email address that received an email. It would look something like this in the log:
    Sent 4 emails.<br> Email sent to user1<br> Email sent to user2<br> Email sent to user3<br> Email sent to user4<br>
    I need to get the $log variable within my existing loop but have it updated with every email. So do I have to run a separate loop within my loop?