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); #### 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);