#!/usr/bin/perl use DBI; my $dbfile = 'test.db'; # your database file my $need2create = ! -f $dbfile; my $dbh = DBI->connect( # connect to your database, create if + needed "dbi:SQLite:dbname=$dbfile", # DSN: dbi, driver, database file "sq", # no user "lite", # no password { RaiseError => 1 }, # complain if something goes wrong ) or die $DBI::errstr; my $table = 'test'; my @rows = qw(id hobby email); if($need2create){ print "Creating $dbfile -> $table \n"; $dbh->do("create table $table (".join(',',@rows).')') or die $DBI +::errstr; my @DATA = ('1|chess|camelot@aorta.com','2|math|dude@oops.no','3|m +ath|foo@bar.com'); for my $data (@DATA){ $str ="insert into '$table' ('".join("','",@rows)."')"." value +s ('".join("','",split(/\|/,$data))."')"; print $str ."\n"; $dbh->do($str) or die $DBI::errstr; } } $sql ="select * from $table"; my $sth = $dbh->prepare($sql); $sth->execute(); my @SENDTO; while (my @row = $sth->fetchrow_array) { # retrieve one row if($row[1]=~/math/i){ print "emailing $row[2]\n"; push(@SENDTO, $row[2]); # print "". join(", ", @row), "\n"; } } for my $who (@SENDTO){ $log = "I have emailed $who\n"; print $log; &log_it; }

Prints:

emailing dude@oops.no emailing foo@bar.com I have emailed dude@oops.no I have emailed foo@bar.com

Alternative is that you concatenate your string with newlines, thus:

my $SENDTO = ""; # start with empty string while (my @row = $sth->fetchrow_array) { # retrieve one row if($row[1]=~/math/i){ $SENDTO .= "emailing $row[2]\n"; push(@SENDTO, $row[2]); } } $log = $SENDTO; &log_it;

In reply to Re: Updating variable in a loop by FreeBeerReekingMonk
in thread Updating variable in a loop by htmanning

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.