Home: Perl Programming Help: DBI:
conditional printing



poochman
Novice


Jun 20, 2009, 1:19 PM


Views: 7902
conditional printing

There is probably a simple solution to this, but I can't find it. I have a loop that goes through about 70 tables; for each table an sql query is created. The query returns one column (the name of a backup table, if it exists). I want to print only the results that contain a table name - not the blank lines.<p> Here is the code:$response = $sth->fetchrow_array(); print "Table = $response\n";<p> I've tried: if ($response != "" ) { print "Table = $reponse\n"; }<p> and: if ($response =~ /^$/) { print "Table = $respone\n"; }<p> Neither of these gives me the results I want; they either print everything, or print nothing at all. What is the correct syntax to only print $response if it contains a value?<p> tia


FishMonger
Veteran / Moderator

Jun 20, 2009, 2:00 PM


Views: 7901
Re: [stonemonolith] conditional printing

Please post a copy/paste of your code wrapped in the code tags and also post sample data retrieved that shows what you want and don't want printed.


(This post was edited by FishMonger on Jun 20, 2009, 2:01 PM)


poochman
Novice


Jun 20, 2009, 5:29 PM


Views: 7893
Re: [FishMonger] conditional printing

Here is the code, and some sample output. I want to only print the lines that contain table1_bak and table2_bak, and ignore the blank lines

$sql = "select rtrim(substring(o.name,1,40))
from sysobjects o, sysusers u
where o.type = 'U' and o.uid = u.uid and u.name = 'dbo'
and o.name like '${tbl}_%'";
$sth = $dbh->prepare($sql);
$sth->execute;

($response) = $sth->fetchrow_array();
print "Table = $response\n";

****************** output ****************
Table =
Table =
Table = table1_bak
Table =
Table = table2_bak


poochman
Novice


Jun 20, 2009, 7:37 PM


Views: 7888
Re: [stonemonolith] conditional printing

I did a little more trial and error coding - I finally figured it out. I used the code print "Table = $response\n" if defined($response);