in reply to if/else options
Something like
would do the trick.my @where; my $sql = 'select * from table'; push @where, 'column1 = "' . $var1 . '" ' if defined $var1; push @where, 'column2 = "' . $var2 . '" ' if defined $var2; push @where, 'column3 = "' . $var3 . '" ' if defined $var3; $sql = $sql . ' where ' . join(' and ', @where);
When you put the values into an array, it would be even better:
my @values = (undef, $var1, $var2, $var3); my @where; my $sql = 'select * from table'; foreach my idx (1..3) { push @where, qq(column$dx = "$values[$idx]") if defined $values[$idx +]; } $sql = $sql . ' where ' . join(' and ', @where);
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: if/else options
by Melly (Chaplain) on Aug 20, 2009 at 11:52 UTC | |
Re^2: if/else options
by dsheroh (Monsignor) on Aug 21, 2009 at 11:41 UTC |
In Section
Seekers of Perl Wisdom